我正在向URL发送HTTP POST请求。它在响应的location
标题中发回我需要的一条信息。我如何获得该标题?我已经尝试了以下代码,它似乎不起作用:
在使用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
标题,以及如何从响应中获取它?
答案 0 :(得分: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);
}
}
}