检查 context.Request.Body 的条件是 azure api 管理策略中的 JArray 或 JObject
Condition to check context.Request.Body is JArray or JObject in azure api management policy
我正在使用 Azure API 管理策略表达式将供应商值发送到每个 post,向后端 API 发出和删除请求。我写了一段代码,当请求类型为 JObject 时,它运行良好。但我有一些请求可以是 JArray 类型的情况,在这种情况下它会抛出 500 错误。以下代码片段适用于 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>
我需要一个可以检查请求正文类型并进行相应解析的条件。否则如果请求正文是 IEnumerable/JArray 类型,那么上面的代码会给我错误。
当我在请求正文中有 IEnumerable
时出现以下错误
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)
你能帮我解决这个问题吗?
不是最干净的解决方案,但可以考虑您在此处的要求。请注意,我现在解析为下面的 JToken,它是 JObject 和 JArray 的基础。然后检查类型后做事
<set-variable name="Supplier" value="DummySupplier" />
<choose>
<when condition="@(new [] {"post", "put", "delete"}.Contains(context.Request.Method, StringComparer.OrdinalIgnoreCase))">
<set-body>@{
JToken body = context.Request.Body.As<JToken>();
if (body.Type == JTokenType.Array)
{
JObject newBody = new JObject();
newBody["OriginalArray"] = body;
newBody["Supplier"] = (string)context.Variables["Supplier"];
return newBody.ToString();
}
if (body.Type == JTokenType.Object)
{
body["Supplier"] = (string)context.Variables["Supplier"];
return body.ToString();
}
return context.Request.Body.ToString();
}
</set-body>
</when>
</choose>
如果我使用 below ,我会得到 below 错误。
第 30 行第 15 列的元素 'set-body' 中的错误:表达式
中不支持类型 'System.Object' 的成员 'ToString' 的使用
我的出境政策是这样的。
<outbound>
<base />
<return-response>
<set-status code="200" reason="Success" />
<set-header name="Accept-Encoding" exists-action="override">
<value>deflate</value>
</set-header>
<set-header name="WWW-Authenticate" exists-action="override">
<value>PatientID Received="PatientKey0001"</value>
</set-header>
<set-body>@{
JToken body = context.Request.Body.As<JToken>();
if (body.Type == JTokenType.Array)
{
}
if (body.Type == JTokenType.Object)
{
}
return context.Request.Body.ToString();
}
</set-body>
</return-response>
</outbound>
我正在使用 Azure API 管理策略表达式将供应商值发送到每个 post,向后端 API 发出和删除请求。我写了一段代码,当请求类型为 JObject 时,它运行良好。但我有一些请求可以是 JArray 类型的情况,在这种情况下它会抛出 500 错误。以下代码片段适用于 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>
我需要一个可以检查请求正文类型并进行相应解析的条件。否则如果请求正文是 IEnumerable/JArray 类型,那么上面的代码会给我错误。
当我在请求正文中有 IEnumerable
时出现以下错误
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)
你能帮我解决这个问题吗?
不是最干净的解决方案,但可以考虑您在此处的要求。请注意,我现在解析为下面的 JToken,它是 JObject 和 JArray 的基础。然后检查类型后做事
<set-variable name="Supplier" value="DummySupplier" />
<choose>
<when condition="@(new [] {"post", "put", "delete"}.Contains(context.Request.Method, StringComparer.OrdinalIgnoreCase))">
<set-body>@{
JToken body = context.Request.Body.As<JToken>();
if (body.Type == JTokenType.Array)
{
JObject newBody = new JObject();
newBody["OriginalArray"] = body;
newBody["Supplier"] = (string)context.Variables["Supplier"];
return newBody.ToString();
}
if (body.Type == JTokenType.Object)
{
body["Supplier"] = (string)context.Variables["Supplier"];
return body.ToString();
}
return context.Request.Body.ToString();
}
</set-body>
</when>
</choose>
如果我使用 below ,我会得到 below 错误。 第 30 行第 15 列的元素 'set-body' 中的错误:表达式
中不支持类型 'System.Object' 的成员 'ToString' 的使用我的出境政策是这样的。
<outbound>
<base />
<return-response>
<set-status code="200" reason="Success" />
<set-header name="Accept-Encoding" exists-action="override">
<value>deflate</value>
</set-header>
<set-header name="WWW-Authenticate" exists-action="override">
<value>PatientID Received="PatientKey0001"</value>
</set-header>
<set-body>@{
JToken body = context.Request.Body.As<JToken>();
if (body.Type == JTokenType.Array)
{
}
if (body.Type == JTokenType.Object)
{
}
return context.Request.Body.ToString();
}
</set-body>
</return-response>
</outbound>