Forum Discussion
F5 iRule for X-Country-Code not working as expected
Is it possible to insert an X-Country-Code into the F5 BIG-IP response to the client.
I want to do this only for a specific URI pattern. I tried the iRule below, using HTTP_REQUEST to capture the country code when the pattern /java is matched and substituting it in the HTTP_RESPONSE, but it didn't work. Any suggestions would be greatly appreciated.
HTTP_REQUEST {
if {[HTTP::uri] starts_with "/java"} {
set country_code [whereis [IP::client_addr] country]
log local0. "Matched /example - client IP: [IP::client_addr], country: $country_code"
# Temporarily store country code in a header for use in HTTP_RESPONSE
HTTP::header insert "X-Country-Temp" $country_code
} }
when HTTP_RESPONSE {
if {[HTTP::uri] starts_with "/java"} {
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"
} }
Also I tried below in the response , but no luck
when HTTP_RESPONSE {
if {[HTTP::header exists "X-Country-Code" ]} {
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"
} }
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
}
}
4 Replies
- Injeyan_Kostas
Nacreous
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 Responsewhen 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" } }
- Jeffrey_Granier
Employee
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:
- ashokp1
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
}
}
- Melissa_C
Moderator
Hello ashokp1,
I see you have a few responses to assist you and wanted to encourage you to update your post if either of these have helped answer your questions. If they have please mark the post as solved, or if it has not please update with where you are at and what you may need additional help with.
Thank you for being a part of our community!
-Melissa
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