Technology Blogs by Members
Explore a vibrant mix of technical expertise, industry insights, and tech buzz in member blogs covering SAP products, technology, and events. Get in the mix!
cancel
Showing results for 
Search instead for 
Did you mean: 
akanksha2203
Explorer
Hello Everyone.....Welcome to my first blog!!!!!!

Introduction-As a beginner in SAP API management ,I have been exploring various policies in SAP API portal . In this blog post ,I have shared Extract variable policy which is used to read and parse information from a request or response and set it to the value of the variables that can be used in API proxy execution.


 Use Cases:-

1.To get access_token using extract variable policy from OAuth2.0 secured proxy API and sending it to another API to have access using service callout policy.



 


2. Getting response from the target endpoint (post flow) and extracting few fields using extract variable policy. In the given example I am extracting value of mobile no and city.

Consider a scenario , wherein  I am calling an API whose response is in the JSON array format.

Sample response:-
[ 
{
"Name":"xyz",
"mobileno": "123456",
"postal-address": {
"streetno": "11",
"city": "New Delhi",
}
}

]

 

In the above response code, mobile no and postal address  is in the 0 pocket of the array  .To extract this value of mobile no and city ,I will be using extract variable policy in the target end point post flow.

 

Sample code will be
<ExtractVariables async="true" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<JSONPayload>
<Variable name="mob">
<JSONPath>$.[0].mobileno</JSONPath>
</Variable>
<Variable name="city">
<JSONPath>$.[0].postal-address.city</JSONPath>
</Variable>
</JSONPayload>
<Source>response</Source>
</ExtractVariables>

 


 

 

I am using assign message policy in target end point post flow to assign the value of mob and city in the Mobileno and City respectively
<!-- This policy can be used to create or modify the standard HTTP request and response messages -->
<AssignMessage async="false" continueOnError="false" enabled="true" xmlns='http://www.sap.com/apimgmt'>
<Set>
<Payload contentType="application/json">{"Mobileno":"{mob}","City":"{city}"}</Payload>
</Set>
<IgnoreUnresolvedVariables>true</IgnoreUnresolvedVariables>
<AssignTo createNew="false" type="response">response</AssignTo>
</AssignMessage>


 


 

Conclusion:-In this way ,we can extract elements from JSON array using extract variable policy and assign it to some variable using assign message policy in SAP API management.

 

That's it from my side. I hope you have enjoyed learning  this blog. Please let me know your feedback or any query in the comment section. and keep following my profile for similar content.

 

.

 
2 Comments
Labels in this area