gpt4 book ai didi

Azure API管理策略: Unable to check if a parameter exists or not in request body

转载 作者:行者123 更新时间:2023-12-03 03:26:16 25 4
gpt4 key购买 nike

我有一个控制流,我在其中指定策略来​​检查请求正文中是否存在 location 属性,如果存在,我将使用 <send-request> 发送请求这是我的代码:

<set-variable name="location-id" value="@((string)context.Request.Body.As<JObject>(preserveContent: true)["location"]["id"])" />                                                
<choose>
<when condition="@(!string.IsNullOrEmpty(context.Request.Body.As<JObject>(true)["location"].Value<string>()))">
<send-request mode="new" timeout="20" response-variable-name="locationid" ignore-error="false">
<set-url>@($"https://api.dev.com/external/location/location/{(string)context.Variables["location-id"]}")</set-url>
<set-method>GET</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>@{ var document = (JObject)context.Variables["newRequest"];
return document?.ToString();
}</set-body>
</send-request>
</when>
<otherwise />
</choose>

这是我的请求正文:

{
"id": 0,
"policyList": [
{
"id": 4,
"number": "string",
"uri": "string"
}
],
"primaryPolicy": {
"id": 23,
"number": "string",
"uri": "string"
},
"isLocationAmountRequired": true,
"currency": 0,

"location": {
"id": 1,
"number": "string",
"postcode": "string",
"name": "string",
"uri": "string"
},
"modifiedOn": "string",
"modifiedBy": {
"adObjectId": "string",
"adUserPrincipalName": "string",
"adName": "string",
"email": "string"
}
}

但是这样做之后我应该得到响应,但我收到 500 内部服务器错误

{
"statusCode": 500,
"message": "Internal server error",
"activityId": "fa17a054-fbfb-477e-9637-276257808c65"
}

我应该怎么做才能解决这个问题

最佳答案

跟踪在失败后返回:

choose (10.095 ms)

{
"messages": [
{
"message": "Expression evaluation failed.",
"expression": "!string.IsNullOrEmpty(context.Request.Body.As<JObject>(true)[\"location\"].Value<string>())",
"details": "Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.\r\n at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)"
},
"Expression evaluation failed. Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken.\r\n at Newtonsoft.Json.Linq.Extensions.Convert[T,U](T token)",
"Cannot cast Newtonsoft.Json.Linq.JObject to Newtonsoft.Json.Linq.JToken."
]
}
  1. 如果 location属性丢失,set-variable将在 choose 之前失败健康)状况。请将这部分移至 choose条件:
    <set-variable name="location-id" value="@((string)context.Request.Body.As<JObject>(preserveContent: true)["location"]["id"])" />
  2. 检查您的请求
    2.1 location缺失:context.Request.Body.As<JObject>(true)["location"] != null
    2.2 location为空:context.Request.Body.As<JObject>(true)["location"].Type != JTokenType.Null
    2.3 完成when声明:<when condition="@(context.Request.Body.As<JObject>(true)["location"] != null && context.Request.Body.As<JObject>(true)["location"].Type != JTokenType.Null)">

3.最后缺失变量newRequest必须在策略中添加:

set-body (0.589 ms)

{
"messages": [
{
"message": "Expression evaluation failed.",
"expression": " var document = (JObject)context.Variables[\"newRequest\"];\n return document?.ToString();\n ",
"details": "The given key was not present in the dictionary.\r\n at System.ThrowHelper.ThrowKeyNotFoundException()\r\n at System.Collections.Generic.Dictionary`2.get_Item(TKey key)"
},
"Expression evaluation failed. The given key was not present in the dictionary.\r\n at System.ThrowHelper.ThrowKeyNotFoundException()\r\n at System.Collections.Generic.Dictionary`2.get_Item(TKey key)",
"The given key was not present in the dictionary."
]
}

完整政策:

<policies>
<inbound>
<base />
<choose>
<when condition="@(context.Request.Body.As<JObject>(true)["location"] != null && context.Request.Body.As<JObject>(true)["location"].Type != JTokenType.Null)">
<set-variable name="location-id" value="@((string)context.Request.Body.As<JObject>(preserveContent: true)["location"]["id"])" />
<send-request mode="new" timeout="20" response-variable-name="locationid" ignore-error="false">
<set-url>@($"https://api.dev.com/external/location/location/{(string)context.Variables["location-id"]}")</set-url>
<set-method>GET</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>
<!-- newRequest is not defined in policy which will cause an error -->
<set-body>@{ var document = (JObject)context.Variables["newRequest"];
return document?.ToString();
}</set-body>
</send-request>
</when>
<otherwise />
</choose>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>

关于Azure API管理策略: Unable to check if a parameter exists or not in request body,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75624345/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com