Forum Discussion
F5 iRule for X-Country-Code not working as expected
- Oct 22, 2025
Thanks Jeffrey_Granier, I tried the code below which resolved my issue. Thanks for your support.
when HTTP_REQUEST {
if {[HTTP::uri] starts_with "/java"} {
set country_code [whereis [IP::client_addr] country]
log local0. "Matched /java - client IP: [IP::client_addr], country: $country_code"
set session_key "country_[TCP::client_port]"
table set $session_key $country_code 300
}
}when HTTP_RESPONSE_RELEASE {
set session_key "country_[TCP::client_port]"
set country_code [table lookup $session_key]
if {$country_code ne ""} {
log local0. "HTTP_RESPONSE triggered for session $session_key, country: $country_code"
HTTP::header insert "X-Client-Country-Code" $country_code
log local0. "Added X-Country-Code: $country_code to response"# Clean up the session table
table delete $session_key
}
}
Hello ashokp1
You are injecting an HTTP Header in Request which will not exist in Response
You should keep the value in a variable and inject it directly in Response
when HTTP_REQUEST
{
if {[HTTP::uri] starts_with "/java"}
{
set country_code [whereis [IP::client_addr] country]
}
}
when HTTP_RESPONSE
{
if {[info exists country_code]}
{
HTTP::header insert "X-Country-Code" "$country_code"
}
}
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