我一直在尝试使用 Azure APIM 将基于授权令牌的请求转发到某个后端。
我能够访问我的应用程序(跟踪显示错误是在尝试将模型解析为 JSON 时发生的),但是当我尝试将请求正文解析为我的模型 (DTO) 时,我收到错误:
Microsoft.AspNetCore.Http.BadHttpRequestException: Failed to read parameter "FooBarModel request" from the request body as JSON.
---> System.Text.Json.JsonException: '0x0A' is invalid within a JSON string. The string should be correctly escaped. Path: $ | LineNumber: 1 | BytePositionInLine: 19.
我的 APIM 政策如下:
<policies>
<inbound>
<base />
<choose>
<when condition="@(context.Request.Headers.GetValueOrDefault("Authorization","").AsJwt()?.Claims.GetValueOrDefault("key", "") == "AuthTokenValue")">
<set-backend-service backend-id="BackendId" />
<set-body template="liquid" xsi-nil="null">
"@(
JObject body = context.Request.Body.As<JObject>();
var newBody = {
"key1": {{body.key1}},
"key2": {{body.key2}},
{
"key3": {{body.key3}},
"key4": {{body.key4}}
}
};
return newBody.ToString();
)"
<set-header name="Content-Type" exists-action="override"><value>application/json</value></set-header></set-body>
<rewrite-uri template="@{
return "/api/v1/end/{value}/point";
}" />
</when>
<otherwise />
</choose>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
我相信这与发送请求正文的数据类型有关,但我似乎找不到解决方案。
请不要发布文档链接,除非它绝对是这个问题的答案,因为我已经一遍又一遍地阅读它。
经过大量研究,我找到了一种方法:
<set-body>@{
var incomingRequestBody = context.Request.Body.As<JObject>();
var outgoingRequestBody = new JObject();
outgoingRequestBody["key1"] = incomingRequestBody["key1"];
outgoingRequestBody["key2"] = incomingRequestBody["key2"];
var nestedDict = new JObject();
nestedQrCodeInfo["nestedKey1"] = incomingRequestBody["nestedKey1"];
nestedQrCodeInfo["nestedKey2"] = incomingRequestBody["nestedKey2"];
outgoingRequestBody["nestedKey"] = nestedDict;
return outgoingRequestBody.ToString();
}
</set-body>
这是一种简单而有效的转换和返回请求正文的方法。