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
}
}
when HTTP_REQUEST {
set insert_country_code 0
if {[HTTP::uri] starts_with "/java"} {
set country_code [whereis [IP::client_addr] country]
set insert_country_code 1
log local0. "Matched /java - client IP: [IP::client_addr], country: $country_code"
}
}
when HTTP_RESPONSE {
if {$insert_country_code == 1} {
log local0. "HTTP_RESPONSE triggered for [IP::client_addr]"
HTTP::header insert "X-Country-Code" $country_code
log local0. "Added X-Country-Code: $country_code to response"
}
}
Hi ashokp1 ,
F5 has a wonderful AI Assistant 🤖, I recommend you try it out. F5 AI Assistant | F5 something like this should accomplish what you are trying to do:
- ashokp1Oct 22, 2025
Altostratus
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
}
}
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