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
Ret. Employee
iRules LX comes with two flavour. One is RPC style as above. It uses both iRules (TCL) and node. The other one is Streaming. It's pure Node without TCL, which became available from v13.0.
Here's a Streaming example. The specification is same as the RPC one.
var f5 = require('f5-nodejs');
var plugin = new f5.ILXPlugin();
// string to json parser (same as RPC version)
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;
}
// Reading buffer and return string
var readMe = function(ilxTransaction) {
var chunk = [];
while(true) {
var buffer = ilxTransaction.read();
if (buffer !== null) {
chunk.push(buffer);
}
else {
break;
}
}
return chunk.join('').toString() || '{"error":"no data"}';
}
// Main loop
plugin.on('connect', function(flow) {
// Started to receive client POST data
var postData = '';
flow.client.on('readable', function(req) {
postData = readMe(flow.client);
});
// Got all the client POST data. Send it over to poolMember
flow.client.on('requestComplete', function(req) {
console.log('client req (POST): ' + postData);
var postDataJson = parser(postData);
postDataJson['newKey'] = 'new';
console.log('req to poolmember: ' + JSON.stringify(postDataJson));
flow.server.write(JSON.stringify(postDataJson));
req.complete();
console.log('res done');
});
// Started to receive poolMember body
var body = '';
flow.server.on('readable', function(res) {
body = readMe(flow.server);
});
// Got all the server data. Push it to client
flow.server.on('responseComplete', function(res) {
console.log('server res (body): ' + body);
var bodyJson = parser(body);
delete bodyJson['aoc'];
console.log('res to client: ' + JSON.stringify(bodyJson));
flow.client.write(JSON.stringify(bodyJson));
res.complete();
});
// Error notification
flow.client.on('error', function(errorText) {
console.log("client error event: " + errorText);
});
flow.server.on('error', function(errorText) {
console.log("server error event: " + errorText);
});
flow.on("error", function(errorText) {
console.log("flow error event: " + errorText);
});
});
var options = new f5.ILXPluginOptions();
// No specific option here
plugin.start(options);
DanSchell
May 24, 2019Nimbostratus
Hi Satoshi,
that means the pure stream example does not need the iRule part, only the XL Workspace js extension?
- Satoshi_Toyosa1May 29, 2019Ret. Employee
Yes, iRules LX Streaming does not require iRules. Instead, you create the profile from the plugin, and attach it to the virtual.
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