...
Code Block | ||||
---|---|---|---|---|
| ||||
rule r1 { select when web pageview url #re#/archives/# http:post("http://www.example.com/go", form = {"answer": "x"}) } |
...
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.
...