David Andrews
David Andrews

Reputation: 81

Return APIM response as valid JSON with liquid template

I am trying to modify a response in with an Azure APIM policy as follows:

<set-body>@{
JObject response = context.Response.Body.As<JObject>(preserveContent: true);
return response.ToString();
}</set-body>

If I use this as the response my json is fine. Howeever, I am trying to nest the result body using a liquid template as follows

<set-body template="liquid">{"result" : { {{body}} }, "targetUrl": "null", "success": "true"}
</set-body>

However the body does not come out as formatted json and is missing commas from the first level of the response.

EG it comes out as:

"code": "1234" "manufacturer": "test" "count": 1

It should be

"code": "1234", "manufacturer": "test" ,"count": 1

I do not want to transform the body just nest it in the result. What is the best way to do this.

TIA

Upvotes: 0

Views: 58

Answers (1)

David Andrews
David Andrews

Reputation: 81

I figured the best way to do this was not to use a liquid template but to stringfy the json and append the result as follows:

<set-body>@(
         "{\r\n    \"result\": " + context.Response.Body.As<string>(preserveContent: true) + ",\r\n    \"targetUrl\": \"null\",\r\n    \"success\": \"true\",\r\n    \"error\": \"null\",\r\n    \"unAuthorizedRequest\": \"false\",\r\n    \"__abp\": \"true\"\r\n}"
                )</set-body>

Upvotes: 0

Related Questions