@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.