...
Code Block | ||||
---|---|---|---|---|
| ||||
rule r2 {
select when web pageview url re#/archives/#
http:post("https://example.com/printenv.pl",
body =
<< <?xml encoding='UTF-8'?>
<feed version='0.3'>
</feed> >>,
headers = {"content-type": "application/xml"});
}
|
Get
The http:get()
action works in the same manner as http:post()
, except that an HTTP GET is used instead of a POST.
...
Code Block | ||||
---|---|---|---|---|
| ||||
url_content = http:get(my_url){"content"}; |
If you're retrieving JSON, you'll need to turn the string in the content field into a data structure using decode():
Code Block | ||||
---|---|---|---|---|
| ||||
json_from_url = http:get(my_url){"content"}.decode(); |
Processing the Response
If autoraise
is set, the rule will automatically raise an event with the event domain http
and the event type equal to the HTTP method used (i.e. post
, put
, get
, delete
). The event will contain attributes with the same name-value structure that HTTP actions and function return.
...
The following simple example shows a rule that has an http:post()
action with an event autoraise:
Code Block | ||||
---|---|---|---|---|
| ||||
rule r1 {
select when web pageview url re#archives/(\d+)# setting(year)
http:post("http://www.example.com/go",
form = {"answer": "x"},
autoraise = "example");
} |
This is roughly equivalent to the following rule:
Code Block | ||||
---|---|---|---|---|
| ||||
rule r1 { select when web pageview url re#archives/(\d+)# setting(year) http:post("http://www.example.com/go", form = {"answer": "x"}) setting (resp) always { raise explicit event "post" attributes resp } } |
The biggest difference is that the autoraise would create an event with the event domain http
while the raise statement would create an event with the event type explicit
.
Assuming we raised the http:post
event in the first rule shown above, we could chain additional rules for subsequent processing of the response. The following two rules check the status code of the response and present a notification of the result:
Code Block | ||||
---|---|---|---|---|
| ||||
rule r2 { select when http post label re#example# status_code re#(2\d\d)# setting (status) send_directive("Status", {"status":"Success! The status is " + status}); } rule r3 { select when http post label re#example# status_code re#([45]\d\d)# setting (status) fired { log error <<#{status}: #{event:attr("status_line")}>>; last; } } |
The second rule fires when the status code in the response indicates an error, logs the error, and uses the last
control statement in the postlude to stop subsequent processing of rules in the ruleset.
This example rule shows the content of the response if its content type is “text.”
Code Block | ||||
---|---|---|---|---|
| ||||
rule r4 {
select when http post label re#example#
if(event:attr("content_type") like "^text/") then
send_directive("Page says...", {"content":event:attr("content")});
} |
Rule chaining from an autoraise on an http:post()
action provides a convenient and simple way of easily dealing with the results of an action.
...