Reputation: 59
I need help building APIM policy for one of my task.
I have two payloads.
One that gets “UniqueID” from an external service, and verifies that webhook is coming from that external service.
The other payload goes in the request body, when sending in the request.
The two payloads are somewhat different, so when I put the payload from the one that is coming from the external service, I get missing body request, because the API expects the another payload in the request body.
What I want APIM to do, is modify(change the name of keys, add new keys) so the payload looks exactly like the one API expects.
Payload1
{
"DocumentID": "221",
"UniqueID": "1001",
"dependentee_name": {
"first": "Tony",
"last": "Stark"
},
"insurer_first_name": "Steve",
"insurer_last_name": "Rogers"
}
Payload2
{
"insurer": {
"firstName": "Steve",
"lastName": "Rogers"
},
"dependentee": {
"firstName": "Tony",
"lastName": "Stark"
}
}
Payload3 - expected
{
"DocumentID": "221",
"UniqueID": "1001",
"insurer": {
"firstName": "Steve",
"lastName": "Rogers"
},
"dependentee": {
"firstName": "Tony",
"lastName": "Stark"
}
}
I know I have to do something with or set variable that gets the body. But I am not sure, how I would add keys and modify its name. Any example of Azure API Management doing this would be very helpful
Upvotes: 0
Views: 792
Reputation: 3937
I'm not sure if I understand your problem, but I can provide you with an example of how to change the JSON object structure.
A new JObject
is created with the new structure and the values from the request body have to be set in the new object.
Therefore I created a POST Operation that transforms the JSON:
Policy:
<policies>
<inbound>
<base />
<return-response>
<set-status code="200" reason="ok" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var body = context.Request.Body.As<JObject>(true);
var transformedBody = new JObject();
transformedBody["DocumentID"] = body["DocumentID"];
transformedBody["UniqueID"] = body["UniqueID"];
var insurer = new JObject();
insurer["firstName"] = body["insurer_first_name"];
insurer["lastName"] = body["insurer_last_name"];
transformedBody["insurer"] = insurer;
var bodyDependentee_name = body["dependentee_name"] as JObject;
var dependentee = new JObject();
dependentee["firstName"] = bodyDependentee_name["first"];
dependentee["lastName"] = bodyDependentee_name["last"];
transformedBody["dependentee"] = dependentee;
return transformedBody.ToString();
}</set-body>
</return-response>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
POST https://rfqapiservicey27itmeb4cf7q.azure-api.net/abc/transform HTTP/1.1
Host: rfqapiservicey27itmeb4cf7q.azure-api.net
{
"DocumentID": "221",
"UniqueID": "1001",
"dependentee_name": {
"first": "Tony",
"last": "Stark"
},
"insurer_first_name": "Steve",
"insurer_last_name": "Rogers"
}
HTTP/1.1 200 ok
content-length: 202
content-type: application/json
date: Fri, 02 Sep 2022 05:05:58 GMT
request-context: appId=cid-v1:a10dc7c9-c354-40a2-acf3-1401681f7808
vary: Origin
{
"DocumentID": "221",
"UniqueID": "1001",
"insurer": {
"firstName": "Steve",
"lastName": "Rogers"
},
"dependentee": {
"firstName": "Tony",
"lastName": "Stark"
}
}
Upvotes: 2