Forum Discussion
Editing JSON HTTP Response
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
}And to do the http to https and remove the port I can just do this?
when HTTP_RESPONSE_DATA {
regsub "http://" [HTTP::payload] "https://" fixeddata
regsub ":80" $fixedddata "" fixeddata1
set payload [string map [list "\}\}\}\}" "\}\}"] [HTTP::payload]]
HTTP::payload replace 0 $clen $payload $fixeddata1
HTTP::release
}
- Kevin_StewartOct 18, 2022
Employee
Is http:// and :80 in the payload or headers?
For example, you'd normally see this in Location headers (redirects) and in payload data referencing document objects. But if in payload, it's probably better to avoid regex and just do multiple string maps.
when HTTP_RESPONSE_DATA { set payload [string map [list "\}\}\}\}" "\}\}"] [HTTP::payload]] set payload [string map [list "http://" "https://"] [HTTP::payload]] set payload [string map [list ":80" ""] [HTTP::payload]] HTTP::payload replace 0 $clen $payload $fixeddata1 HTTP::release }What you're doing is grabbing the existing payload into a variable, making changes to that variable, and then completely replacing the HTTP::payload with the contents of that variable.
- Chris_BatesOct 19, 2022
Nimbostratus
Just to confirm that this works for you to replace the brackets?
We've just implemented this change and it still doesn't want to replace them...
- Kevin_StewartOct 19, 2022
Employee
Try this:
when HTTP_RESPONSE_DATA { log local0. "payload before: [HTTP::payload]" set payload [string map [list "\}\}\}\}" "\}\}"] [HTTP::payload]] set payload [string map [list "http://" "https://"] [HTTP::payload]] set payload [string map [list ":80" ""] [HTTP::payload]] log local0. "payload after: $payload" HTTP::payload replace 0 $clen $payload $fixeddata1 HTTP::release }Tail the LTM log:
tail -f /var/log/ltmAnd send a request. This should show you the payload before and after manipulation.
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