无法在Azure API管理中使用返回-响应策略返回响应

31moq8wy  于 2023-01-31  发布在  其他
关注(0)|答案(1)|浏览(167)

我有这样的情况:在出站中,我将检查API策略所在的GET API的响应。如果主体有一个entityStatus值为0或1的键,如果为0,则输出简单消息,否则需要发送请求到另一个API端点(POST API)。
我使用的API策略如下:

<set-variable name="id" value="" />
    <set-variable name="newRequest" value="@(context.Request.Body?.As<JObject> 
           (preserveContent: true))" />
    <choose>
     <when condition="@(context.Response.StatusCode == 200 && 
     (int)context.Response.Body?.As<JObject>(true)["entityStatus"] == 1)">
    <send-request mode="new" timeout="20" response-variable-name="id" ignore-error="false">
        <set-url>@($"https://api.dev/external/workrequest/wr")</set-url>
        <set-method>POST</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>
    <return-response response-variable-name="id">
        <set-status code="200" reason="OK" />
        <set-header name="Content-Type" exists-action="override">
            <value>application/json</value>
        </set-header>
        <set-body>@((((IResponse)context.Variables["id"]).Body.As<JObject>()).ToString())</set-body>
    </return-response>
</when>
<when condition="@(context.Response.StatusCode == 200 && (int)context.Response.Body?.As<JObject>(true)["entityStatus"] == 0)">
    <return-response>
        <set-status code="500" reason="VOID" />
        <set-header name="Content-Type" exists-action="override">
            <value>application/json</value></set-header>
        <set-body>The record is void</set-body>
     </return-response>
</when>
<otherwise />

当entityStatus为0时,它返回的响应是正确的,但当entityStatus为1时,它返回了一个400错误。

{
"type": "https://httpstatuses.io/400",
"title": "Bad Request",
"status": 400,
"traceId": "00-317b53ba7a76f19b6153ca6ab14c5190-e9c27cf9162e2414-00"
 }

我只需要来自POST API的响应,我将请求发送到该API,我该如何做到这一点?

fae0ux8s

fae0ux8s1#

我得到了一个解决方案,我正在使用下面的变量,将采取请求正文:
<set-variable name="newRequest" value="@(context.Request.Body?.As<JObject>(preserveContent: true))" />
我使用以下命令更改了它:
set-variable name="newRequest" value="@(context.Response.Body.As<JObject>(preserveContent: true))" />

相关问题