简体   繁体   中英

How to force apigee proxy to rewrite response URLs from target URL to proxy URL with base path

I've created proxy for my REST API:

When you call target endpoint directly, example:

POST http://products.example.com/items 

you'll get response with location header like this

http://products.example.com/items/123131

But if I go through proxy:

POST http://example.apigee.net/products/v2/items 

then location header for newly created resource still points to target URL:

http://products.example.com/items/3423423

But I expected to get

http://example.apigee.net/products/v2/items/3423423

The question is: how to configure proxy to rewrite URLs in response to Proxy URLs?

Not sure if this helps much but I raised a very similar question to yours at How do you correctly return Location headers and do HATEOAS with Apigee? . We ended up needing to write our own policy which does a simple search and replace on the response received from the target server. It would be nice if something was provided out of the box.

Apigee proxy allows you to add policies in the response part of a flow. Example below:

<Flows>
        <Flow name="myFlow">
            <Description></Description>
            <Request/>
            <Response>
               <Step>
                    <Name>ModifyLocationHeaderPolicy</Name>
               </Step>
            <Response/>
        </Flow>
</Flows>

I suggest you to add a javascript or python or java callout policy which will read the Location header of the response and re-write it. Something to the effect like below in Javascript

locationHeader = context.getVariable("message.header.Location");
// modify it
 context.setVariable("message.header.Location", locationHeader);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM