Forum Discussion

CarloMun's avatar
CarloMun
Icon for Nimbostratus rankNimbostratus
May 17, 2019

iRule for parsing the body / payload of a post request and adding an extra block of content to the originally received body

Hi,

 

I have been asked to setup the following behaviour on an BIG IP F5 and I wonder if it is possible

 

(I'm not a developer guy - so apologies for any possible incorrect infos I might state here ...)

 

Here's the setup:

 

a virtual server (LTM 13.1.0 only licensed) is supposed to receive a POST Request JSON enconded and subsequently:

 

a) collect the entire POST Request (ca. 30 MB)

b) parse the POST Request Headers, select a Pool dynamically based on a value listed in the POST Request

c) modify the body of the original POST Request adding some context (fixed content) at the end of the original POST Request.

 

I already managed to configure and iRULE capable of successfully deal with Points a) and b).

 

The question is if c) is possible and what tools I might use.

 

Thanks

2 Replies

  • There's a couple of ways you can do this, if the POST is JSON like you say, the easiest way (in my opinion) is to use iRules LX. iRules LX is essentially Node.js which is server side JavaScript.

     

    I wrote a code share article on reading in JSON body and modifying the values of some of the keys. You can take a similar approach to add new key pair values

     

    https://devcentral.f5.com/s/articles/simple-iruleslx-json-rewrite-1196

     

    The alternative, is to read in the body as before and look for unique bit of code, then using ```string map``` to replace replace this code with the bit you were looking for + the new inserted bit.

    Also remember to rewrite your Content-Length header.

     

    Hopefully this will be enough to get you going:

     

    set payload [HTTP::payload]
    set find "\"key1\": \"value1\""
    set insert  "$find, \"key2\": \"value2\""
    set newPayload [string map [list "$find" "$inset"] $payload ]
    HTTP::payload replace 0 [HTTP::header "Content-Length"] $newPayload

     

    Good Luck!

    Lee

  • Hi,

    I am faced with the same problem with you,I want to implements the function like a) and b),can you share your irule about a) and b) ?

    Thanks!