Forum Discussion

raj_77723's avatar
raj_77723
Icon for Nimbostratus rankNimbostratus
Feb 08, 2016

how to insert the header in http request and response on specific condition met

We need to insert a custom header for both HTTP request and response if the request contains some specific parameter, both request and response should have that additional header inserted.

 

1 Reply

  • Hi Raj,

    depending on how the specific parameter is passed, the code would look very differently.

    If the parameter is a simple [HTTP::uri] query string, the code would look like that...

     

    when HTTP_REQUEST {
        if { [URI::query [HTTP::uri] "MY_URI_PARAMETER"] equals "VALUE_XYZ" } then {
            HTTP::header insert "HEADER_XYZ" "VALUE_XYZ"
            set header_insert 1 
        }       
    }
    when HTTP_RESPONSE {
        if { [info exist header_insert] } then {
            HTTP::header insert "HEADER_XYZ" "VALUE_XYZ"
            unset -nocomplain header_insert
        }
    }
    

     

    But if the specific parameter is passed via POST data, then you have to [HTTP::collect] those data frist to enumerate the contained query string information.

    Note: Since [HTTP::collect] do have a certain performance impact, it would be recommended to have some additional [if] conditions in place, to identify the request URIs that need to be collected. So if the data is based on POST data, then please provide additional information on how to identifiy those request (e.g. certain [HTTP::path], [HTTP::uri] information, etc.).

    Cheers, Kai