Extract path variable from URL and set in target base path dynamically

Not applicable

Hi All,

1) i have below setup in proxy endpoint flow

<HTTPProxyConnection>

<BasePath>/application/tworder/v1/ip-customers/*/ip-users/</BasePath> <Properties/> <VirtualHost>secure</VirtualHost>

</HTTPProxyConnection>

2) i have below setup in target endpoint flow

<HTTPTargetConnection>

<LoadBalancer>

<Server name="appTargetURL"/> </LoadBalancer>

<Path>/application/tworder/v1/ip-customers/123456789/ip-users/</Path></HTTPTargetConnection>

How do i replace the value 1234567890 dynamically? i tried with java-script policy

var a=context.getVariable("apigee.customerId");

var b="/application/ipss/v1/ip-customers/"+a+"/ip-users/";

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

Extract Policy

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-path-variable-from-request">

<DisplayName>Extract-Path-Variable-From-Request</DisplayName> <Properties/>

<URIPath name="customerId">

<Pattern ignoreCase="true">/{customerId}</Pattern> </URIPath>

<Source clearPayload="false">request</Source>

<VariablePrefix>apigee</VariablePrefix>

</ExtractVariables>

Output:- customer is still null

http://ip-userms.sdpamp.com/application/tworder/v1/ip-customers/null/ip-users/
0 8 5,670
8 REPLIES 8

According to mentioned extract variable policy , you should be hitting

https://{apigee.host}/application/tworder/v1/ip-customers/*/ip-users/{customerId}

to make this work .As URI path in apigee does not contain base path , even though it is a URI fragment , it's just a string that uniquely identifies the URI path used by Apigee Edge to route incoming messages to the proper API proxy much like a sever's context path.

If your customerId is present at wildcard in base path , then you don't have to go through all this trouble to extract customer id and assign it to target url , as apigee uri and target uri have 1-1 mapping and apigee uses the same URI to hit target backend if there is no modification. You just need to use basepath and target path properly.

<HTTPProxyConnection>
	<BasePath>/application/tworder/v1</BasePath>
	<Properties/> 
	<VirtualHost>secure</VirtualHost>
</HTTPProxyConnection>
<HTTPTargetConnection>
	<LoadBalancer>
		<Server name="appTargetURL"/> 
	</LoadBalancer>
	<Path>/application/tworder/v1</Path>
</HTTPTargetConnection>

In this case if you hit apigee path

https://{apigee-host}/application/tworder/v1/ip-customers/{customerId}/ip-users/

request will go to ,

https://{target-host}/application/tworder/v1/ip-customers/{customerId}/ip-users/

you can also use conditional flows to apply any other policies required for this, like extracting customer Id

<Flows>
        <Flow name="name">
            <Description/>
            <Request>
                <Step>
                    <Name>EV-ExtractCustomerId</Name>
                </Step>
            </Request>
            <Response/>
            <Condition>(proxy.pathsuffix MatchesPath "/ip-customers/{customerId}/ip-users/")</Condition>
        </Flow>
 </Flows>
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ExtractVariables async="false" continueOnError="false" enabled="true" name="Extract-path-variable-from-request">
	<DisplayName>Extract-Path-Variable-From-Request</DisplayName> 
	<Properties/>
	<URIPath>
		<Pattern ignoreCase="true">/ip-customers/{customerId}/ip-users/</Pattern> 
	</URIPath>
	<Source clearPayload="false">request</Source>
	<VariablePrefix>apigee</VariablePrefix>
</ExtractVariables>

Now , this will set proper value of apigee.customerId in context.

Hope this will help !!

If i understood correctly, javascript policy is not required?

if your customerId is at * in mentioned basepath , then nope.Just need to remove it from basepath so apigee can take it as URI and forward it to target.

Thanks lot Amit. let me try and update you.. FYI. I have other resources under same API with different verb... your suggestion works for all below resources

Base path :/application/tworder/v1/ip-customers/{customerId}/ip-users

POST : /application/tworder/v1/ip-customers/{customerId}/ip-users

POST : /application/tworder/v1/ip-customers/{customerId}/ip-users/{userName}/assignService

POST : /application/tworder/v1/ip-customers/{customerId}/ip-users/{userName}/unassignService

GET : /application/tworder/v1/ip-customers/{customerId}/ip-users

GET : /application/tworder/v1/ip-customers/{customerId}/ip-users/{userName} GET : /application/tworder/v1/ip-customers/{customerId}/ip-users/count

PUT : /application/tworder/v1/ip-customers/{customerId}/ip-users/{username}

PATCH :/application/tworder/v1/ip-customers/{customerId}/ip-users/{userName}/lock PATCH :/application/tworder/v1/ip-customers/{customerId}/ip-users/{userName}/unlock PATCH :/application/tworder/v1/ip-customers/{customerId}/ip-users/{username}/passwordreset

PATCH :/application/tworder/v1/ip-customers/{customerId}/ip-users/lock?userNames={userNames};

PATCH :/application/tworder/v1/ip-customers/{customerId}/ip-users/unlock?userNames={userNames};

DELETE :/application/tworder/v1/ip-customers/{customerId}/ip-users/{userName}

DELETE :/application/tworder/v1/ip-customers/{customerId}/ip-users?userNames={userNames};

DELETE:/application/tworder/v1/ip-customers/{customerId}/ip-users/unAssignService

Yes ,apigee uses same URI and Verb for target , unless it's changed explicitly. so it should work for all url's if you intending to keep proxy's verb and URI same as target's.

I followed same step you mentioned, but getting some error, am i missing something

Hit url => https://slot2.org003.t-dev.net/application/tworder/v1/ip-customers/1234567890/ip-users

Response:- path": "/application/tworder/v1/ip-customers/1234567890/ip-users"

Are you able to see target hit in trace ? If that's the case then , something is missing in request.Please also check that target server "appTargetUR" is properly configured in environment.

can you provide us trace session , it will be easier to pin point the issue

Deployment url:- https://slot2.org002.t-dev.net/application/tworder/v1

appending with ip-customers/1234567890/ip-users

finalurl:- https://slot2.org002.t-dev.net/application/tworder/v1/ip-customers/1234567890/ip-users

I cant see the trace itself...when i hot via postman , result as below

{
"timestamp": 1526283500020,
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/application/tworder/v1/ip-customers/1234567890/ip-users"
}