Azure APIM -返回字节数据的set-body策略

9wbgstp7  于 2023-11-21  发布在  其他
关注(0)|答案(1)|浏览(103)

我一直在尝试使用Azure APIM将基于授权令牌的请求转发到某个后端。
我能够到达我的应用程序(跟踪显示错误是在尝试将Model解析为JSON时),但是当我尝试将请求体解析为Model(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&lt;JObject&gt;();
                    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>


我相信这与发送请求体的数据类型有关,但我似乎找不到解决方案。
请不要张贴链接到文档,除非它绝对是这个问题的答案,因为我已经读了一遍又一遍。

qvsjd97n

qvsjd97n1#

经过大量的研究,我找到了一种方法来做到这一点:

<set-body>@{
  var incomingRequestBody = context.Request.Body.As<JObject>();
  // get the incoming RequestBody and set it as a JSON object.

  var outgoingRequestBody = new JObject();
  // create a new JSON object, one that will be the new RequestBody.

  outgoingRequestBody["key1"] = incomingRequestBody["key1"];
  // set the new RequestBody keys from the old RequestBody key values.

  outgoingRequestBody["key2"] = incomingRequestBody["key2"];

  var nestedDict = new JObject();
  // in case you have a nested object, such as a dictionary in your payload, 
  // it is necessary to create a new JSON Object and store the desired values in the created object.

  nestedQrCodeInfo["nestedKey1"] = incomingRequestBody["nestedKey1"];
  nestedQrCodeInfo["nestedKey2"] = incomingRequestBody["nestedKey2"];
  // setting the nested dictionary keys values (for example) 
  
  outgoingRequestBody["nestedKey"] = nestedDict;
  // setting the nested dictionary to a key on the new RequestBody to be returned.
  
  return outgoingRequestBody.ToString();
  // it is necessary to return the new Body.
}
</set-body>

字符串
这是一种简单而有效的转换和返回请求体的方法。

相关问题