1
votes

I'm making a request using the <send-request> policy, and I need to check the Status Code of the request. To do this, I'm trying to access the StatusCode property of the response-variable-name. However, when using this code -

context.Variables["LogFailedEvent"].StatusCode

I'm seeing this error -

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

According to the 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.

Either I'm misinterpreting what is in the docs, or the docs are wrong. Is anyone able to suggest a solution?


Relevant section from policy

<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>
1

1 Answers

4
votes

You need to cast it:

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

Also, since youre setting ignore-error="true" you might want to check first is Variables contains this key at all, since in that case this variable is notcreated at all if request fails.