Azure APIM How to assign Header response to a variable

Thomas, Gregory 5 Reputation points
2023-02-01T16:40:40.1666667+00:00

I have the following policy fragment that connects to an API we are using for authorization.

<fragment>
    <send-request mode="new" response-variable-name="bearerToken" timeout="20" ignore-error="true">
        <set-url>{{url}}</set-url>
        <set-method>POST</set-method>
        <set-header name="Content-Type" exists-action="override">
            <value>application/x-www-form-urlencoded</value>
        </set-header>
        <set-body>@{ return "{{target-url}}"; }</set-body>
    </send-request>
    <set-header name="Authorization" exists-action="override">
        <value>@("Bearer " + (String)((IResponse)context.Variables["bearerToken"]).Body.As<JObject>()["access_token"])</value>
    </set-header>
</fragment>

This works fine with no issues, however, I would like to capture the information pushed to the Authorization Header and push it to a variable for later use in my APIs.

When I do something similar to the following or it's variants, I only receive errors and no information as to what has happened.

<set-variable name="BearerToken" value="@("Bearer " + (String)((IResponse)context.Variables["bearerToken"]).Body.As<JObject>()["access_token"]" />

I'm not sure if this is an order of execution or something else I might have missed?

Thank you - Greg.

Azure API Management
Azure API Management
An Azure service that provides a hybrid, multi-cloud management platform for APIs.
1,651 questions
0 comments No comments
{count} vote

1 answer

Sort by: Most helpful
  1. MuthuKumaranMurugaachari-MSFT 21,646 Reputation points
    2023-02-02T14:22:46.81+00:00

    @Thomas, Gregory Thanks for posting in Microsoft Q&A. Based on my understanding, you were able to access response object via bearerToken variable and assigning it to Authorization Header also worked fine in the policy fragment. However, you are looking to reuse this bearer token in the actual policy (not policy fragment) and facing the error. Is that correct?

    Make sure you have the policy fragment executed first, and then look for variable in your actual policy. As you may know, the policies are statements that run sequentially on the request and response. Instead of trying to read entire IResponse, it might be easier to have set-variable in your policy fragment with variable BearerToken and set it in header as well as use it in your actual policy if possible.

    <fragment>
    	<!--<set-variable name="myVar" value="Test Value" />-->
        <send-request mode="new" response-variable-name="bearerToken" timeout="20" ignore-error="true">
            <set-url>{{url}}</set-url>
            <set-method>POST</set-method>
            <set-header name="Content-Type" exists-action="override">
                <value>application/x-www-form-urlencoded</value>
            </set-header>
            <set-body>@{ return "{{target-url}}"; }</set-body>
        </send-request>
        <set-variable name="BearerToken" value="@("Bearer " + (String)((IResponse)context.Variables["bearerToken"]).Body.As<JObject>()["access_token"])" />
        <set-header name="Authorization" exists-action="override">
            <value>@((String)context.Variables["BearerToken"])</value>
        </set-header>
    </fragment>
    

    Can you check if that works for you? If you still face issues, please share the actual policy snippet to understand better.

    1 person found this answer helpful.