azure - 访问状态代码或使用 <send-request> 发出的请求

标签 azure azure-api-management

我正在使用 <send-request> 发出请求政策,我需要检查请求的状态代码。为此,我尝试访问 response-variable-name 的 StatusCode 属性。 。但是,当使用此代码时 -

context.Variables["LogFailedEvent"].StatusCode

我看到这个错误 -

Error in element 'set-body' on line 83, column 4: 'object' does not contain a definition for 'StatusCode' and no extension method 'StatusCode' accepting a first argument of type 'object' could be found

根据docs -

The response-variable-name attribute is used to give access the returned response. The name defined in this property can be used as a key into the context.Variables dictionary to access the IResponse object.

要么我误解了文档中的内容,要么文档是错误的。有人可以提出解决方案吗?

<小时/>

政策相关部分

<choose>
    <when condition="@(context.Response.StatusCode != 201 && context.Response.StatusCode != 202)">

        <send-request mode="new" response-variable-name="LogFailedEvent" timeout="20" ignore-error="true">
            <set-url>@(string.Format("{0}{1}?code={2}&statusCode={3}&statusReason={4}&message={5}", "{{BVT_LogEventsFailedFunction_Url}}", "{{BVT_LogEventsFailedFunction_Template}}", "{{BVT_LogEventsFailedFunction_Code}}", context.Response.StatusCode, context.Response.StatusReason, context.Response.Body.As<string>()))</set-url>
            <set-method>POST</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/json</value>
            </set-header>
            <set-body>@(context.Request.Body.As<string>())</set-body>
        </send-request>

        <set-body>@(string.Format("{{\"message\":\"event captured by LogFailedEvents\",\"requestId\":\"{0}\",\"statusCode\":\"{1}\",}}", context.RequestId.ToString(), context.Variables["LogFailedEvent"].StatusCode))</set-body>

    </when>
</choose>

最佳答案

您需要转换它:

((IResponse)context.Variables["LogFailedEvent"]).StatusCode

此外,由于您设置了ignore-error =“true”,您可能需要首先检查变量是否包含此键,因为在这种情况下,如果请求失败,则根本不会创建此变量。

关于azure - 访问状态代码或使用 <send-request> 发出的请求,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45548508/

相关文章:

azure - SignalR Actor 或无状态服务

sql-server - 使用 SQL Server Management Studio 远程连接到 Azure 虚拟机上托管的 SQL Server Express 实例

azure - 在 Azure API 管理中,我可以使用订阅 key 作为请求 header 的一部分而不是查询字符串参数吗?

azure - 通过 Azure API 管理连接时,WebSocket 服务在收到客户端消息后始终关闭连接

javascript - process.env 中的用户定义键在 JavaScript Azure Functions 中未定义

azure - Azure 可以连接到非 -"Azure Connect"IPsec VPN 吗?

azure - 如何访问 Azure Application Insights 中的页面 View ?

azure - 如何登录azure函数和api管理

azure - 将多个产品添加到 Azure API 管理服务上的订阅

azure - 如何获取 token 以从具有托管身份的 Function App 访问 APIM