Forward Client IP Address to Target Server

Not applicable

I want to forward Client's IP Address in HTTP request to target server, currently I see APIgee's IP address. I am using following C# code to get IP Address from the HTTP request. HttpContext.Current.Request.UserHostAddress

1 4 2,066
4 REPLIES 4

@vishal.savsani

You can get the client ip using flow variable "client.ip" . For more details find here. You can forward same to the target via headers using Assign Message policy.

Set a new custom header and assign value using flow variable "client.ip" in Assign Message Policy. Read same in your C# code using request headers.

Thanks Anil for the information, but I don't want to set any extra header for the same. Actually, I have logic spread in my application and third party library using "HttpContext.Current.Request.UserHostAddress" to get caller's IP address.

I already did the same thing you explained, Here is the policy,

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Assign-Message-1">
    <DisplayName>Assign Message-1</DisplayName>
    <Properties/>
    <Add>
        <Headers>
            <Header name="ClientIpAddress">{client.ip}</Header>
        </Headers>
        <QueryParams />
        </QueryParams>
        <FormParams/>
    </Add>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="true" transport="http" type="request"/>
</AssignMessage>

Few observations from above policy code, It creates a new request payload since assignto Tag createNew attribute is set to true. Ideally it should be false.

You can read headers from HttpContext class. For more details find here.

Anil,

It should be "false" but it doesn't create any issues. I know we can read header but the problem is that we can't modify third party library. I just want to know is there any way we can change HTTP request properties and all that using policy or proxy settings, so that HTTP caller information should be forwarded to target without any change.