Apigee - modify target path when using targetserve

2019-06-02 18:53发布

问题:

I have an API proxy that needs to be able to modify the path component of the eventual Target URL. This appears fairly straightforward in cases where there's a defined Target Endpoint URL for the entire proxy but we are using TargetServers and VirtualHosts which apparently are assigned after the TargetEndpoint PreFlow

I have a JS function in the Target Endpoint PreFlow and get unspecified JS errors when I attempt to modify context.targetRequest.path. Attempts to modify the path part of the incoming proxy during Proxy PreFlow also failed.

Dumping the values of targetRequest gives:

  1. host=empty
  2. path=/v2/cat1/cat2/?param=......
  3. url=Identical to path!

The only variable that I've been able to "successfully" modify is targetRequest.url to achieve my aim but to do that I must assign the whole thing, including the protocol and host which aren't known to me!

Anyone know how to do this? I essentially want to modify the path replacing "/?" with just "?"

Thanks

回答1:

You can also set it up at the target endpoint by leveraging the Path element along with TargetServer:

<TargetEndpoint>
    ...
    <HTTPTargetConnection>
        <LoadBalancer>
            <Server name="TargetServerABC"/>
        </LoadBalancer>        
        <Path>/v1/YourPathHere/json.ws?{flow.company.queryparams}</Path>
    </HTTPTargetConnection>
</TargetEndpoint>

Note TargetServerABC needs to be a TargetServer created using the following these steps.



回答2:

There seems to be an issue with manipulating target.path (there is a bug report open in Apigee for this).

The current workaround is to rewrite the entire URI either in the AssignMessage policy using the AssignVariable block:

<AssignVariable>
    <Name>target.url</Name>
    <Value>http://example.com/mypath?param=value&variable={apigee.variable}</Value>
    <Ref/>
</AssignVariable>

or you can do it in Javascript with the context.setVariable function:

context.setVariable("target.url", mycompleteurl);


标签: proxy apigee