EDUCAÇÃO E TECNOLOGIA

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:

  1. Should familiarized the standard option as stated above blog
  2. In Mediation Policies, Add the Extract Variable policy
  3. Define the numerous target end points in APITargetEndPoint
  4. 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%20of%20APIProxy%20with%20TargeEndPoint%20Folder

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%20Policy%20and%20View%20ProxyEndpoint

Import Policy and View ProxyEndpoint

Inspect the variable:

Debug%20of%20the%20APIM%20Steps%20-%20Extract%20variable

Debug of the APIM Steps – Extract variable

Notice below POST call matching Step2 above of Condition String

Testing%20with%20Postman

Testing with Postman

Acknowledge:  Thanks to my colleague Vinayak Adkoli to provided the first POC;

Additional references

  1. https://blogs.sap.com/2019/05/27/dynamic-routing-using-sap-cloud-platform-api-management/
  2. https://answers.sap.com/questions/13039092/extract-variable-from-soap-payload-in-api-proxy.html