2011-06-14 85 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