Scenario - you have a simple API with GET requests that aren't very RESTful.
e.g.
GET /something/blah/dogs/id/123/breed/dalmation
and you want to convert it to
GET /dogs/123?breed=dalmation
A simple way to implement this uses two Apigee policies.
Firstly, an Extract Variables policy can use a regular expression to obtain the variables we want.
ExtractPathVariables.xml
<ExtractVariables name="ExtractPathVariables"> <DisplayName>Extract URL variables</DisplayName> <Source>request</Source> <URIPath> <Pattern ignoreCase="true">/dogs/{dogId}</Pattern> </URIPath> <QueryParam name="breed"> <Pattern ignoreCase="true">{dogBreed}</Pattern> </QueryParam> <VariablePrefix>urirequest</VariablePrefix> <IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables> </ExtractVariables>
Next we can create the new URL. There are two options.
We can use an Assign Message policy if we have very simple logic.
AssignNewUrl.xml
<AssignMessage name="AssignNewUrl"> <AssignTo createNew="false" type="request">request</AssignTo> <Set> <Path>/something/blah/dogs/id/{urirequest.dogId}/breed/{urirequest.dogBreed}</Path> </Set> <IgnoreUnresolvedVariables>false</IgnoreUnresolvedVariables> </AssignMessage>
If our logic is more complex, we can use a Javascript policy.
//some logic here to work out the url /* ... */ var baseUrl = context.getVariable("https://httpbin.org"); var dogId = context.getVariable("urirequest.dogId"); var dogBreed = context.getVariable("urirequest.dogBreed"); //set the url context.setVariable("target.url", baseUrl + "/something/blah/dogs/id/" + dogId + "/breed/" + dogBreed);
Do we need to set target.copy.pathsuffix to false?