0

I have been facing an issue while setting a variable in azure policy.

What I have to do is to set currency based on the responses from the backend

response I (status 200) { "currency" : "usd", "amount" : 500 }

Response II (status 200) { "status" :"no data available" }

What I tried:

<set-variable name="currency" value="@((string)(context.Response.Body.As<Jobject>(preserveContent:true).["currency"]))"/>

<set-variable name="currency" value="@((string)(context.Response.Body.As<Jobject>(preserveContent:true).selectToken ("currency"))"/>

<set-variable name="currency" value="@((string)(context.Response.Body.As<Jobject>(preserveContent:true).selectToken ("currency"))"/>

<set-variable name="currency" value="@((string)(context.Response.Body.As<Jobject>(preserveContent:true).GetValueOrDefault<string>.["currency"]))"/>

All of them results in error in case of Response II

What would be the right way to handle if it does not exists?

xt1135
  • 3
  • 2
  • Does this answer your question? [APIM context.Response.Body always null](https://stackoverflow.com/questions/71943001/apim-context-response-body-always-null) – Ecstasy Aug 08 '22 at 04:23
  • [Condition to check context.Request.Body is JArray or JObject in azure api management policy](https://stackoverflow.com/questions/63899022/condition-to-check-context-request-body-is-jarray-or-jobject-in-azure-api-manage) and [Checking for empty or null JToken in a JObject](https://stackoverflow.com/questions/24066400/checking-for-empty-or-null-jtoken-in-a-jobject) – Ecstasy Aug 08 '22 at 04:25

0 Answers0