I am using Azure API Management policy expression to send the Supplier value into each post, put and delete request to backend API. I wrote a code which was working fine when the request type of JObject. but I have some case where the request can be type of JArray, in that case it throw 500 error. The below snippet is working fine for JObject.
<set-variable name="Supplier" value="DummySupplier" />
<choose>
<when condition="@(new [] {"post", "put", "delete"}.Contains(context.Request.Method, StringComparer.OrdinalIgnoreCase))">
<set-body>@{
JObject body = context.Request.Body.As<JObject>();
body.Add(new JProperty("Supplier", ((string)context.Variables["Supplier"])));
return body.ToString();
}
</set-body>
</when>
</choose>
I need a condition where I can check the request body type and parse accordingly. Otherwise in case of if request body is IEnumerable/JArray type, then above code gives me error.
Getting below error when I have an IEnumerable
in request body
The message body is not a valid JSON. Error reading JObject from JsonReader. Current JsonReader item is not an object: StartArray. Path '', line 1, position 1.
at Newtonsoft.Json.Linq.JObject.Load(JsonReader reader, JsonLoadSettings settings)
at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.AsJObject(Stream stream, Encoding encoding, JsonSerializerSettings settings)
at Microsoft.WindowsAzure.ApiManagement.Proxy.Gateway.MessageBody.As[T](Boolean preserveContent)
Could you please help me out with this?