Skip to Content
Technical Articles
Author's profile photo Raghavendra Kulkarni

Dynamic Routing using SAP Cloud Platform API Management

This blog describes the Dynamic routing use case using SAP Cloud Platform API Management.

Dynamic routing helps to define multiple target endpoints for an APIProxy and also set the condition to select the target endpoint at runtime

The use case is to create an API Proxy which will route to different target endpoints at runtime depending on a defined condition.

We are going to create an APIProxy and define the following two target endpoints  :

  • https://sapes5.sapdevcenter.com/sap/opu/odata/IWFND/CATALOGSERVICE
  • https://services.odata.org/V2/OData/OData.svc/

The first step is to launch APIPortal and create an APIProxy using any one of the above urls as the target endpoint

Download the newly created APIProxy to define target endpoints and set conditions

 

There is a parent folder APIProxy with the following folder structure :

Open the folder APITargetEndPoint to define the target endpoints

There will be an XML file called default.xml, delete the default.xml file

Define target endpoints and add target endpoint definition files in APITargetEndPoint folder

Let’s define the Categorries.xml to hit the target endpoint https://services.odata.org/V2/OData/OData.svc/  

As shown in the above screenshot, in the Categories.xml file provide a name to the target endpoint and set the backend URL

Similarly, define the ServiceCollection.xml to hit the target endpoint https://sapes5.sapdevcenter.com/sap/opu/odata/IWFND/CATALOGSERVICE

We have defined the TargetEndPoints, now let’s add the condition using Route Rule

To define the Route Rule open the default.xml file present in the APIProxyEndPoint folder

As shown in the above screenshot, in the default.xml file provide a route rule name and set the Conditions

Route rule Categories checks the set condition proxy.pathsuffix MatchesPath “/Categories”

when the path suffix is /Categories, URL defined in the TargetEndPoint Categories will be hit at runtime

Similarly, define the Route rule ServiceCollection

Finally, open the API_Proxy_Name.xml (in this example APIProxy name is Multiple_Target_End_Point.xml)

Add the defined target endpoints in the targetEndPoints tag

 

Almost done, let’s import the modified APIProxy zip and open the APIProxy in the APIPortal.

Go to Proxy EndPoint tab and add the Route Rules as shown in the above screenshot and save

 

Now to see how dynamic routing works, hit the APIProxy URL with pathSuffix /Categories

Response:

 

 

Validate the response, lets hit the target endpoint(backend URL) with pathSuffix /Categories

Response:

Both the responses match 🙂  APIProxy created successfully with multiple target endpoints..

Similarly hit the APIProxy & Target Endpoint(corresponding backend) URL with the path suffix /ServiceCollection and validate the response

 

Assigned Tags

      4 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Hemachandan A S
      Hemachandan A S

      Thanks for the detailed steps so hoping to see these options in the portal level soon. 🙂

      Cheers,

      Chandan

      Author's profile photo Philipp Sattler
      Philipp Sattler

      Hi Raghavendra,

      unfortunately, when I tried your tutorial, I couldn’t import the API Proxy. Neither was I able to update the existing Proxy (Error when importing it: Unable to update API Proxy from zip content.), nor was I able to import the Proxy after I deleted the orginal Proxy (the upload got stuck).

      Any ideas?

      Regards,

      Philipp

      Author's profile photo Raghavendra Kulkarni
      Raghavendra Kulkarni
      Blog Post Author

      Hi Philipp,

       

      Can you please give me a little more details of the error.

      Did you try the same steps with the same name or you have different API Proxy name

       

      Regards,

      Raghavendra Kulkarni

      Author's profile photo Soham Basu
      Soham Basu

      Hi Raghavendra,

       

      I have followed the steps exactly what you have mentioned, to expose an API for 2 different HTTPS services from CPI . All details are looking exactly same as per your screenshot, however when I am trying to access the API along with path suffixes, I am getting 404:Not Found error. Not able to identify where I have gone wrong. The only difference between your scenario and mine is, I am using basic authentication policy in my API proxy preflow. But hope that doesn't make any difference.

       

      Thanks,

      Soham