2011-06-14 83 views
2

我正在向URL发送HTTP POST请求。它会在响应的location标题中发回我需要的一条信息。我如何获得该标题?我试着下面的代码,它似乎并没有工作:KRL:从http:post()获取“位置”标题()

在使用该http:post()行动规则的动作块:

http:post("https://cas.byu.edu/cas/v1/tickets/") 
    with params = {"username": netid, "password": password} 
    and autoraise = "gottgt" 
    and response_headers = ["location"]; 

规则处理该事件http

rule got_tgt { 
    select when http post label "gottgt" 
    pre { 
     content = event:param("content"); 
     location = event:param("location"); 
    } 
    { 
     notify("CAS Login", "Got back the POST response (#{location}): #{content}") with sticky=true; 
    } 
} 

但是,location变量始终为空。我如何告诉KRL我需要location标题,以及如何从响应中获取它?

回答

2

尽管我无法测试您的特定端点,但我已经构建了一个示例应用程序,您将在调试此问题时发现它们很有用。

请注意,我既自动调整响应,又使用setting语法来提升事件。你通常不会这样做,但它有点不同。当明确提高结果时,你会得到整个回应。您可以在我的示例中看到server标题已返回,并且也显示在自动标定的规则中。

你的代码看起来不错,但是我会做一个明确的提出并检查响应,就像我在这里展示的那样,这将帮助你确切地知道你可以得到什么。

运行这个程序,在这里:这里http://ktest.heroku.com/a8x183

和代码:

ruleset a8x183 { 
    meta { 
     name "Testing Response Headers" 
     description << 

     >> 
     author "Sam Curren" 
     logging off 
    } 

    dispatch { 
     // domain "example.com" 
    } 

    global { 
     bodylog = defaction(title, msg){ 
      { 
      append("body", "<h1>#{title}</h1>"); 
      append("body", "<div>#{msg}</div>"); 
      } 
     }; 
    } 

    rule first_rule { 
     select when pageview ".*" setting() 
     pre { 

     } 
     http:post("http://httpbin.org/post") setting (res) 
      with params = {"username":"yahuda","password":"metalcages"} 
      and autoraise = "kickstand" 
      and response_headers = ["server"]; 
     fired { 
      raise explicit event "moon" with res = res; 
     } 
    } 

    rule exp { 
     select when explicit moon 
     pre { 
      res = event:param("res"); 
      res_s = res.encode(); 
     } 
     bodylog("explicit raise: full response", res_s); 
    } 

    rule response { 
     select when http post label "kickstand" 
     pre { 
      server_header = event:param("server"); 
      content = event:param("content"); 
     } 
     { 
      bodylog("autoraise: content", content); 
      bodylog("autoraise: server_header", server_header); 
} 
    } 
} 
+0

谢谢!提高明确的事件表明信息在那里。如果我无法弄清楚如何以通常的方式做到这一点,我会这么做。 – 2011-06-14 22:49:24

+0

+1使用httpbin.org – 2011-06-14 23:17:29

+0

但我的雅赫达月亮参考没有额外的积分?认真。 – TelegramSam 2011-06-16 15:53:34