F5 iRule for Header insert

Hello,

I create that iRule below, but my header value for the header x-id is still empty, what could be wrong ?

when HTTP_REQUEST {
foreach header_name [HTTP::header names] {
if {([HTTP::header value $header_name] eq “x-id”)} {
set VALUE [HTTP::header “XXX#YYYY” ]
HTTP::header insert “x-id” $VALUE
  }
 }
}

kind regards

eLeCtRoN

it seems you want to replace the header value with something else? What do you want to accomplish exactly?

You can try this.

if { [HTTP::header exists x-id] } {
set VALUE [HTTP::header “XXX#YYYY”]
log command to determine if VALUE is set correctly
log local0. “x-idlog var VALUE=$VALUE”
# header x-d allready exists → remove it first.
HTTP::header remove x-id
# add header x-id with variable VALUE
HTTP::header insert x-id $VALUE
}

The problem might be the var VALUE is not set correctly, and is thus empty.

Hello Cypher,

our header x-id is empty per default and the WAF doesen’t like that and blocks our request, so I want to set a value

for the header x-id, now I set your iRule active and the header is still empty, in the system log I got just the messages

HTTP :header insert x-id $VALUE
syslog-ng log local0. \“x-idlog var VALUE=$VALUE\”

i would troubleshoot ‘bottom-up’ and use lowercase for variables (not sure if that has an impact, but it is a good practice).

i don’t know what XXX#YYYY is, but it needs to exists as a header, or whatever you use instead of XXX#YYYY. so lets see if it exists first in the logs.

when HTTP_REQUEST {
foreach header_name [HTTP::header names] {
log local0. “$header_name”
 }
if { [HTTP::header exists x-id] } {
set value [HTTP::header “XXX#YYYY”]
log command to determine if VALUE is set correctly
log local0. “x-idlog var value=${value}”
# header x-id allready exists → remove it first.
HTTP::header remove x-id
# add header x-id with variable value
HTTP::header insert x-id $value
}
}

check system > logs > local traffic for errors and logs.

Hi @eLeCtRoN do you also have a header named XXX#YYYY? If not, then that’s going to be an empty value anyway. If you are trying to:

  1. Assure the x-id header exists
  2. Assure that the x-id header has a value

Then, something like this should work for you:

when HTTP_REQUEST priority 500 <span class="token punctuation">{</span>
    <span class="token builtin">if</span> <span class="token punctuation">{</span> <span class="token punctuation">[</span>HTTP::header exists x<span class="token operator">-</span>id<span class="token punctuation">]</span> <span class="token operator">&&</span> <span class="token punctuation">(</span><span class="token punctuation">[</span>HTTP::header x<span class="token operator">-</span>id<span class="token punctuation">]</span> <span class="token operator">==</span> <span class="token string">""</span><span class="token punctuation">)</span> <span class="token punctuation">}</span> <span class="token punctuation">{</span> 
        HTTP::header replace x<span class="token operator">-</span>id <span class="token string">"some value here"</span> 
    <span class="token punctuation">}</span> <span class="token builtin">else</span> <span class="token punctuation">{</span> 
        HTTP::header insert x<span class="token operator">-</span>id <span class="token string">"some value here"</span> 
    <span class="token punctuation">}</span> 
<span class="token punctuation">}</span>

if you don’t need the header to be there then you can eliminate the else statement altogether.

Hi Jason,

just short tell you what I have, I have form the application the header x-id with no value inside (empty), so my WAF policy don’t like that and block the request, what I now want to do is just to set a value inside the header x-id. Thanks for your response

then that should work for you as is, or without the else block.