Forum Discussion

Chris_Bates's avatar
Chris_Bates
Icon for Nimbostratus rankNimbostratus
Oct 17, 2022

Editing JSON HTTP Response

Hey guys!

I'm completely new to F5 and am currently trying to fix an issue that we are having with 'HTTP' to 'HTTPS' rewrites. 

There was another post that fixed somewhat the issue that we are facing linked here: Solved: iRULE regsub error - DevCentral

The iRule that this person used doesn't seem to be changing our HTTP response?...

Here is our current rewrite here: Current iRule rewrite - Pastebin.com 

 

If anyone could help that would be amazing!

Thanks 😀

  • You shouldn't need to do an HTTP::collect to handle response headers. Those can be seen and manipulated directly within the HTTP_RESPONSE event.

    • Chris_Bates's avatar
      Chris_Bates
      Icon for Nimbostratus rankNimbostratus

      Okay, so would HTTP::collect be required in the HTTP_RESPONSE_DATA? or can they still be seen?

       

      The response data stays the exact same:

      {"result":{"pathTo3DJSON":"/PackWorks/tempfiles/DOC2865386461295932248/data.cbor"}}}}

      with or without the iRule even though we are supposedly replacing the 4 closing brackets with 2...

      • Kevin_Stewart's avatar
        Kevin_Stewart
        Icon for Employee rankEmployee

        I'm saying that if you need to change HTTP headers, you don't need to do an HTTP::collect and handle in HTTP_RESPONSE_DATA. The headers will be available in the HTTP_RESPONSE event. You would do an HTTP::collect if you're trying to replace text in the response payload (after the headers).

        But I see maybe you're trying to replace headers AND payload. Again, headers are visible in HTTP_RESPONSE, so change those there. For the payload, without testing I'm assuming there's just something incorrect about your string replace logic. The following works for me to replace the ending "}}}}":

        when HTTP_REQUEST {
            set doWrite 0
            if { [string tolower [HTTP::path]] eq "/website/generate3dview.wcr" } {
                set doWrite 1
            }
        }
        when HTTP_RESPONSE {
            if { ( [info exists doWrite] ) and ( $doWrite ) } {
                HTTP::collect [HTTP::header Content-Length]
                set clen [HTTP::header Content-Length]
                set doWrite 0
            }
        }
        when HTTP_RESPONSE_DATA {
            set payload [string map [list "\}\}\}\}" "\}\}"] [HTTP::payload]]
            HTTP::payload replace 0 $clen $payload
            HTTP::release
        }