How to Redirect an HTTP Request with the Payload Body

In some scenarios API proxies may require to redirect incoming requests to a different target server with the same request payload. In such scenarios, we could use an AssignMessage policy similar to following for redirecting an HTTP request received by an API proxy to a different endpoint including the HTTP body:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AssignMessage async="false" continueOnError="false" enabled="true" name="Set-Response-Message">
    <DisplayName>Set-Response-Message</DisplayName>
    <Properties/>
    <Copy source="request">
        <Headers/>
        <QueryParams/>
        <FormParams/>
        <Payload/>
    </Copy>
    <Set>
        <StatusCode>301</StatusCode>
        <Headers>
            <Header name="Location">
                https://httpbin.org/post
            </Header>
        </Headers>
    </Set>
    <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
    <AssignTo createNew="false" transport="http" type="request"/>
</AssignMessage>

Above AssignMessage policy could be engaged in the response flow as follows:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ProxyEndpoint name="default">
    <PreFlow name="PreFlow">
        <Request/>
        <Response>
            <Step>
                <Name>Set-Response-Message</Name>
            </Step>
        </Response>
    </PreFlow>
    <Flows/>
    <PostFlow name="PostFlow">
        <Request/>
        <Response/>
    </PostFlow>
    <HTTPProxyConnection>
        <BasePath>/hello-redirect</BasePath>
        <VirtualHost>secure</VirtualHost>
    </HTTPProxyConnection>
    <RouteRule name="noroute"/>
</ProxyEndpoint>

An example API request and output:

curl -i --post301 -L -d "param1=value1" https://${org}-${env}.apigee.net/hello-redirect

HTTP/1.1 301 Moved Permanently
Date: Tue, 09 Feb 2021 07:26:03 GMT
Content-Type: application/x-www-form-urlencoded
Content-Length: 13
Connection: keep-alive
Host: imesh-eval-test.apigee.net
X-Forwarded-For: ---masked---
X-Forwarded-Port: 443
X-Forwarded-Proto: https
User-Agent: curl/7.64.1
Accept: */*
Location: https://httpbin.org/post

HTTP/2 200
date: Tue, 09 Feb 2021 07:26:04 GMT
content-type: application/json
content-length: 433
server: gunicorn/19.9.0
access-control-allow-origin: *
access-control-allow-credentials: true
{
"args": {},
"data": "",
"files": {},
"form": {
"param1": "value1"
},
"headers": {
"Accept": "*/*",
"Content-Length": "13",
"Content-Type": "application/x-www-form-urlencoded",
"Host": "httpbin.org",
"User-Agent": "curl/7.64.1",
"X-Amzn-Trace-Id": "Root=---masked---"
},
"json": null,
"origin": "---masked---",
"url": "https://httpbin.org/post"
}


Please note that in this example I have set the redirection URL to https://httpbin.org/post and it is a public endpoint available on the Internet. If you are trying this example please change that to a known target server URL as that endpoint will receive the data being posted in the request.

Comments
suleymanay
Bronze 1
Bronze 1

OK

Version history
Last update:
‎02-08-2021 11:41 PM
Updated by: