Customizing API Response
This example demonstrates the way an API response can be customized based on client requirements. The JSON below is a web service that returns a branch office address.
Use Case
Target Response
{"id":102,"name":"Border","streetAddress":"123 MG Road","city":"Banglore","state":"KN","zipCode":"500050"}
The requirement is to customize the response so that it does not contain the ID, then JSON elements must be named 'BranchName' instead of name and Location as a combination of streetAddress, city, and State. The expected response is given below.
Expected Proxy Response
{
"BranchName":"Border",
"Location":"123 MG Road,Banglore, KN"
}
Policies used in the Use Case
- Assign Variable Policy - to parse the response from the Target Server and define variables corresponding to different parts, add in the scope TARGET_RESPONSE.
- Build Message Policy - to customize the JSON returned by the API and to include part of the original request, add to the scope PROXY_RESPONSE.
Steps to achieve the Use Case
Parse the Response from Target Service
The response received from the Target Server is parsed and various elements in the JSON are saved as context variables using the Assign Variable Policy. Source option 'PAYLOAD' is used for this purpose with JSON Path to retrieve respective elements.
Build the Proxy Response from Parsed Variables
The variables populated in the step above are used to build/customize the response from the proxy server. The payload is modified using the Build Message policy. The configuration is displayed below.
Expand for sample text
{
"BranchName":"<Name>",
"Location":"<StreetAddress>,<City>, <State>"
}
Working with JSON
Since the symbols { and } are reserved in JSON, the characters < and > are used as a variable prefix and a variable suffix respectively.