Forum Discussion
Using iRules for JSON response
I am looking for examples of how to set up the LTM to respond to JSON requests using an iRule. When we send back the HTML response page it is locking up our vendors system, so we would like to change the response page. Any help would be appreciated!
5 Replies
Are you saying you want to respond with JSON to the client? If so, you can build the JSON response and issue a HTTP::respond and passing back a content type of "application/json". Check out the HTTP::respond documentation for examples.
-Joe
- Chris_123510
Nimbostratus
Joe,
No, I am already sending a plain HTML response, that needs to stay in place. I need to find a way to answer with a JSON response, and I was hoping that I would be able to do that with an iRule. So from the ASM, I need to be able to send a plain HTML for IE users, an XML for SOAP users, and a HTML for JSON users. I need to find a way to send all three types of responses.
Thanks, Chris
- Kevin_Stewart
Employee
You'd have to escape the special characters, but the method should be the same. Example:
when HTTP_REQUEST { set json "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"A meta-markup language, used to create markup languages such as DocBook.\", \"GlossSeeAlso\": \[\"GML\", \"XML\"\] }, \"GlossSee\": \"markup\" } } } } }" HTTP::respond 200 content $json "Content-Type" "application/json" } - Sam_Richman_263Historic F5 Account
From my understanding, the requirement here is to present JSON content back to the requestor in the event that the JSON request triggers an ASM blocking page. What about something like the following:
when HTTP_REQUEST { if { [HTTP::header "Content-Type"] contains "json" } { set json_content 1 } else { set json_content 0 } } when ASM_REQUEST_BLOCKING { if { $json_content } { HTTP::header remove Content-Length HTTP::header insert header_1 value_1 set response "{ \"glossary\": { \"title\": \"example glossary\", \"GlossDiv\": { \"title\": \"S\", \"GlossList\": { \"GlossEntry\": { \"ID\": \"SGML\", \"SortAs\": \"SGML\", \"GlossTerm\": \"Standard Generalized Markup Language\", \"Acronym\": \"SGML\", \"Abbrev\": \"ISO 8879:1986\", \"GlossDef\": { \"para\": \"Request has been blocked.\", \"GlossSeeAlso\": \[\"GML\", \"XML\"\] }, \"GlossSee\": \"markup\" } } } } }" ASM::payload replace 0 [ASM::payload length] "" ASM::payload replace 0 0 $response } } - Chris_123510
Nimbostratus
Ok, so the following is the iRule that we have in the LTM now. We are seeing HTTP REQUEST REST DETECTED, but we are not see any REQUEST_BLOCKING messages in the logs. If someone could help in finding why this is not functioning complete, it would be greatly appreciated. We are running on 11.2.0 HF1.
when HTTP_REQUEST { if { [HTTP::uri] contains "rest" } { set rest_content 1 log local0. "HTTP REQUEST REST DETECTED" } else { set rest_content 0 } }
when ASM_REQUEST_BLOCKING { log local0. "ASM REQUEST BLOCK: REST DETECTED= $rest_content" if { $rest_content } {
set response "Page blocked"ASM::payload replace 0 [ASM::payload length] "" ASM::payload replace 0 0 $response}
}
Help guide the future of your DevCentral Community!
What tools do you use to collaborate? (1min - anonymous)Recent Discussions
Related Content
* Getting Started on DevCentral
* Community Guidelines
* Community Terms of Use / EULA
* Community Ranking Explained
* Community Resources
* Contact the DevCentral Team
* Update MFA on account.f5.com
