Technical Articles
APIM Dynamic Routing w. Payload or Header
What: There is good blog already on how to deploy Dynamic Routing by Raghavendra, this blog is about how to pass the routing logic via payload or header instead of context path.
Why: Having sender to code logic to context path is not always possible and you may want to keep the existing URI context path consistent and simple
How:
- Should familiarized the standard option as stated above blog
- In Mediation Policies, Add the Extract Variable policy
- Define the numerous target end points in APITargetEndPoint
- Deploy and Test
In the below example where I show routing to various wiki of countries base on payload country to direct wiki
2. Extract Variable from payload
<!-- Extract content from the request or response messages, including headers, URI paths, JSON/XML payloads, form parameters, and query parameters -->
<ExtractVariables async="true" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<!-- the source variable which should be parsed -->
<Source>request.content</Source>
<!-- Specifies the XML-formatted message from which the value of the variable will be extracted -->
<JSONPayload>
<Variable name="country">
<JSONPath>$.country</JSONPath>
</Variable>
</JSONPayload>
</ExtractVariables>
3. Define Various Target Endpoints
Unzip of APIProxy with TargeEndPoint Folder
targetf example file for France
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TargetEndPoint xmlns="http://www.sap.com/apimgmt">
<name>targetf</name>
<url>https://en.wikipedia.org/wiki/France</url>
<provider_id>NONE</provider_id>
<additionalAPIProviders/>
<isDefault>false</isDefault>
<properties/>
<faultRules/>
<preFlow>
<name>PreFlow</name>
</preFlow>
<postFlow>
<name>PostFlow</name>
</postFlow>
<conditionalFlows/>
<loadBalancerConfigurations>
<isRetry>false</isRetry>
<healthMonitor>
<isEnabled>false</isEnabled>
</healthMonitor>
</loadBalancerConfigurations>
</TargetEndPoint>
4. Deploy and review the mapping
Import Policy and View ProxyEndpoint
Inspect the variable:
Debug of the APIM Steps – Extract variable
Notice below POST call matching Step2 above of Condition String
Testing with Postman
Acknowledge: Thanks to my colleague Vinayak Adkoli to provided the first POC;
Additional references
- https://blogs.sap.com/2019/05/27/dynamic-routing-using-sap-cloud-platform-api-management/
- https://answers.sap.com/questions/13039092/extract-variable-from-soap-payload-in-api-proxy.html
I've already used this approach. There is one very big drawback, however. Target policies that are edited in the UI are only changed for the default target endpoint, not for the other target endpoints.