http-headerskrl

KRL: Getting the "location" header from an http:post()


I'm sending an HTTP POST request to a URL. It sends back a piece of information I need in the location header of the response. How do I get that header? I've tried the following code and it doesn't seem to work:

In the action block of the rule that uses the http:post() action:

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

The rule that handles the http event:

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;
    }
}

However, the location variable is always blank. How do I tell KRL that I want the location header, and how do I get it from the response?


Solution

  • While I can't test your specific endpoint, I've built a sample app that you will find useful in debugging this issue.

    Note that I'm both autoraising the response, and using the setting syntax to raise the events. You wouldn't normally do both, but it hilights a difference. When explicitly raising the result, you get the entire response. You can see in my example that the server header is returned, and shown also in the autoraised rule.

    Your code looks right, but I'd do an explicit raise and inspect the response as I show here, and that will help you know exactly what is available to you.

    Run this app here: http://ktest.heroku.com/a8x183

    and code here:

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