简体   繁体   English

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

[英]Unable to return a response using return-response policy in Azure API management

I have a condition like: In the outbound I will check the response of the GET API which is where aAPI policies are there.我有这样的条件:在出站中,我将检查 GET API 的响应,这是 aAPI 策略所在的位置。 If the body has a key with entityStatus which takes values 0 or 1, if 0 then output simple message else need to send-request to another API endpoint which is POST API.如果主体有一个带有 entityStatus 的键,它的值为 0 或 1,如果为 0,则 output 简单消息,否则需要将请求发送到另一个 API 端点,即 POST API。

I used API policies as below:我使用 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 />

The response it returned was good when entityStatus was 0 but when entityStatus is 1 it was returning a 400 error.当 entityStatus 为 0 时,它返回的响应很好,但当 entityStatus 为 1 时,它返回 400 错误。 The error is like:错误是这样的:

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

I just need the response from the POST API which I am sending the request to and how can I do this?我只需要发送请求的 POST API 的响应,我该怎么做?

I got a solution, I was using below variable that will take request body:我有一个解决方案,我正在使用下面的变量来获取请求正文:

<set-variable name="newRequest" value="@(context.Request.Body?.As<JObject>(preserveContent: true))" />

I changed it by using:我通过使用更改它:

set-variable name="newRequest" value="@(context.Response.Body.As<JObject>(preserveContent: true))" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM