如何提取发送请求策略的响应状态代码

时间:2018-04-04 20:00:36

标签: azure-api-management

在Azure APIM中使用发送请求策略。 能够得到响应并提取身体。 但是,努力提取response.status.code

如果发送请求,此@(context.Response.StatusCode)的值为200,我正在尝试捕获此值...

send-request (88 ms)
{
    "response": {
        "status": {
            "code": 200,
            "reason": "OK"
        }
    }
}

1 个答案:

答案 0 :(得分:0)

Hope below solution will work for you.

<choose>
 <when condition="@(((IResponse)context.Variables["response-variable-name"]).StatusCode == 200)">
       <set-variable name="xxx" value="@(((IResponse)context.Variables["response-variable-name"]).Body.As<JObject>(preserveContent: true).ToString())" />
</when>
<otherwise>
    <return-response>
      <set-status code="404" reason="Not Found" />
      <set-body>@(((IResponse)context.Variables["response"]).Body.As<JObject>(preserveContent: true).ToString())</set-body>
    </return-response>
 </otherwise>
</choose>