I am using this code for azure APIM policies
<set-variable name="newRequest" value="@(context.Request.Body?.As<JObject>(preserveContent: true))" />
<set-variable name="insured-id" value="@(context.Request.MatchedParameters["id"].Last())" />
<send-request mode="new" timeout="20" response-variable-name="id" ignore-error="false">
<set-url>@($"https://api.dev.com/external/workRequest/get")</set-url>
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-header name="Authorization" exists-action="override">
<value>@(context.Request.Headers.GetValueOrDefault("Authorization","scheme param"))</value>
</set-header>
<set-body>{"insuredId": @($"{(string)context.Variables["insured-id"]}")}</set-body>
</send-request>
<choose>
<when condition="@((int)((IResponse)context.Variables["id"]).Body.As<JObject>(preserveContent: true)["workRequests"]["entityStatus"]== 1)">
<return-response response-variable-name="id">
<set-status code="400" reason="VOID" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value></set-header>
<set-body>{"statusCode": 400,
"message": "The insured cannot be voided as it is currently attached with one or more active workrequest"}</set-body>
</return-response>
</when>
<otherwise />
</choose>
I am taking an insuredId from template parameter of the API operation where I am implementing the APIM policies & using it in the set-body, this will list all the workrequests for that insuredId.
The payload for the POST is something like
{"insuredId": template-parameter}
When returning a response getting 500 error. How to resolve this. The condition which is there is okay. I am suspecting error in set body.
Also how to check whether a particular string like "entityStatus": 1
is there in the response of api, because this https://api.dev.com/external/workRequest/get
url will give a list of workrequest records in array form