Forum Discussion

Viv_Richards's avatar
Viv_Richards
Icon for Cirrostratus rankCirrostratus
Oct 01, 2024

Need to change HTML tag in HTTP response

Dear Team,

If anybody need to change HTML tag or something similar to below in HTTP Response, you can refer it.

For example, I would like to change "name" with "XYZ" in HTTP response, you can use below iRule

===================== 

when HTTP_REQUEST {
#To avoid HTTP/1.0 to chunked encoding and to prevent compression remove Accept-Encoding 
    HTTP::version 1.0
    HTTP::header remove Accept-Encoding
}
 
when HTTP_RESPONSE {
#Before collecting HTTP response, check whether content type is HTML or not
    if {[HTTP::header Content-Type] contains "text/html"} {
        # Collect up to 1MB of the response body
        HTTP::collect [expr 1024*1024]
    }
}
 
when HTTP_RESPONSE_DATA {
#Ensure it is HTML Content
    if {[HTTP::header Content-Type] contains "text/html"} {
        # First replacement: replace "name=" with "XYZ="
        set find "name="
        set replace "XYZ="
        set new_response [HTTP::payload]
 
#Replace payload
        if {[regsub -all $find $new_response $replace new_response] > 0} {
            HTTP::payload replace 0 [HTTP::payload length] $new_response
        }
 
    }
}
========================= 
 
No RepliesBe the first to reply