My use case is I have a path variable someID coming in the request, for example "example/example/someID/example" , which I want to extract and use later in target endpoint as a path variable. What is the standard recommended way to do this in Apigee. As of now what I have known is to use Extract variable policy. But how it use it ?
Solved! Go to Solution.
You can use the extract variable policy to capture the path parameter in the URL into a variable and use the same to set the target path.
Here is an example - extract the someID from the URIPath into a variable named id
<URIPath> <Pattern ignoreCase="true">/example/example/{id}/example</Pattern></URIPath>You can then use an AssignMessage policy or JS policy to add that to your target url or use <Path> in the target endpoint to append it to the target path
<AssignVariable><Name>target.url</Name><Value>http://httpbin.org/get/{id}</Value></AssignVariable>
or add the Path in the HttpTargetConnection if you are using a LoadBalancer configuration
<Path>/get/{id}</Path>
You can use the extract variable policy to capture the path parameter in the URL into a variable and use the same to set the target path.
Here is an example - extract the someID from the URIPath into a variable named id
<URIPath> <Pattern ignoreCase="true">/example/example/{id}/example</Pattern></URIPath>You can then use an AssignMessage policy or JS policy to add that to your target url or use <Path> in the target endpoint to append it to the target path
<AssignVariable><Name>target.url</Name><Value>http://httpbin.org/get/{id}</Value></AssignVariable>
or add the Path in the HttpTargetConnection if you are using a LoadBalancer configuration
<Path>/get/{id}</Path>
I tried the first part you've mentioned. Then I am using JS policy to print, this is just to confirm if it's making it through. I am getting null for someID.
Sorry, missed the path variable part of the question when I answered above. Nagashree's answer is correct -- make sure the pattern only includes the parts of the path after the proxy's BasePath.
Some how stil now working. Following is my code for ExtractPolicy.
<ExtractVariables async="false" continueOnError="false" enabled="true" name="ev-extract-some-id"> <DisplayName>ev-extract-some-id</DisplayName> <Properties/> <URIPath> <Pattern ignoreCase="true">mypath/mypath/{someID}/mypath</Pattern> </URIPath> <QueryParam name="name"/> <Header name="name"/> <FormParam name="name"/> <Variable name="name"/> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> <Source clearPayload="false">request</Source> </ExtractVariables>
Please provide more details - what's your complete URL, basepath and pathsuffix?check the attached sample and the trace.extractpathparams-rev1-2019-03-04.ziptrace-1551727457453.txt
Thanks for sharing sample.I found the issue. My pattern was missing "/" in the path if you see the code I have posted in the previous comment. Thanks Mike, the parts were not fully matching.
<Pattern ignoreCase="true">/mypath/mypath/{someID}/mypath</Pattern>
Hmm, didn't notice the missing slash in your policy. Glad you figured out from the sample.