KRL:从http:post()获取“location”标题

时间:2011-06-14 21:44:55

标签: http-headers krl

我正在向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标题,以及如何从响应中获取它?

1 个答案:

答案 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);
}
    }
}