Forum Discussion
Ben_9010
Jan 12, 2018Nimbostratus
iRule ReWrite - Inserting JSON Parameter into Request + Removing Same JSON Parameter from Response
Looking to leverage an iRule to Dynamically insert a JSON Parameter into an HTTP payload and then scrub it in the response back to the client.
However, we want to inspect the Payload and only pe...
Satoshi_Toyosa1
Jul 17, 2018Ret. Employee
Yes, iRules LX would be a good option as it can handle JSON objects natively.
Here's a sample code. It adds the key-value {"newKey":"new"} to the request data (POST), and removed the key "aoc" from the response body. This is just a skeleton code. You can add any conditions you want (such as checking the presence of a certain key etc).
iRule part
when HTTP_REQUEST {
set RPC_HANDLE [ILX::init RemoveKeyPlugin RemoveKeyExt]
if { [HTTP::method] eq "POST"} {
HTTP::collect [HTTP::header "Content-Length"]
}
}
when HTTP_REQUEST_DATA {
set payload [HTTP::payload]
set req [ILX::call $RPC_HANDLE req $payload]
log local0. "$payload > $req"
HTTP::payload replace 0 [HTTP::header "Content-Length"] $req
}
when HTTP_RESPONSE {
HTTP::collect [HTTP::header "Content-Length"]
}
when HTTP_RESPONSE_DATA {
set payload [HTTP::payload]
set res [ILX::call $RPC_HANDLE res $payload]
log local0. "$payload > $res"
HTTP::payload replace 0 [HTTP::header "Content-Length"] $res
}
Node (extension) part
var f5 = require('f5-nodejs');
var ilx = new f5.ILXServer();
var parser = function(str) {
var json = {"error":"unable to parse"};
try {
json = JSON.parse(str);
}
catch(e) {
console.log('Got non-Json data: ' + e + ' >>> ' + str);
}
return json;
}
// Adding an entry
var addEntry = function(req, res) {
var json = parser(req.params()[0]);
json['newKey'] = 'new'; // Adding a new entry
res.reply(JSON.stringify(json));
};
// Removing an entry
var removeEntry = function(req, res) {
var json = parser(req.params()[0]);
delete json['aoc'];
res.reply(JSON.stringify(json));
}
ilx.addMethod('req', addEntry);
ilx.addMethod('res', removeEntry);
ilx.listen();
Example output
Jul 17 16:59:04 ltm1310 info tmm[17714]: Rule /Common/RemoveKeyPlugin/RemoveKeyRule : {"hello":"world"} > {"hello":"world","newKey":"new"}
Jul 17 16:59:04 ltm1310 info tmm[17714]: Rule /Common/RemoveKeyPlugin/RemoveKeyRule : { "name":"Chateau Mouton Rothchild", "aoc":"Pauillac", "year":1978 } > {"name":"Chateau Mouton Rothchild","year":1978}
Recent Discussions
Related Content
DevCentral Quicklinks
* 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
Discover DevCentral Connects