I made a policie artifact for an api in API Management, but had a problem.
Error message detail
I tried many times to find the issue, but couldn’t. I perceived a behavior when I change from Standard V2 to Consumption or Developer. It’s worked! Probality, the version v2 have a problem.
Look at the code below.
<policies>
<inbound>
<choose>
<when condition="@(context.Request.Headers.ContainsKey("access-token"))">
<choose>
<when condition="@(context.Request.Headers["access-token"][0].StartsWith("Bearer "))">
<send-request mode="new" response-variable-name="validationResponse">
<set-url>https://its-example-url</set-url>
<set-method>POST</set-method>
<set-header name="Authorization" exists-action="override">
<value>@(context.Request.Headers["access-token"][0].Replace("Bearer ", ""))</value>
</set-header>
</send-request>
</when>
<otherwise>
<return-response>
<set-status code="400" reason="Bad Request" />
<set-header name="Content-Type" exists-action="override">
<value>text/plain</value>
</set-header>
<set-body>Esta faltando o Bearer no valor da chave 'access-token', na header!</set-body>
</return-response>
</otherwise>
</choose>
</when>
<otherwise>
<choose>
<when condition="@(context.Request.Url.Query.ContainsKey("access_token"))">
<send-request mode="new" response-variable-name="validationResponse">
<set-url>https://its-example-url</set-url>
<set-method>POST</set-method>
<set-header name="Authorization" exists-action="override">
<value>@(context.Request.Url.Query.GetValueOrDefault("access_token", ""))</value>
</set-header>
</send-request>
</when>
<otherwise>
<return-response>
<set-status code="400" reason="Bad Request" />
<set-header name="Content-Type" exists-action="override">
<value>text/plain</value>
</set-header>
<set-body>Esta faltando o parametro access_token!</set-body>
</return-response>
</otherwise>
</choose>
</otherwise>
</choose>
<choose>
<when condition="@{
var response = context.Variables.GetValueOrDefault<IResponse>("validationResponse", null);
return (int)response.StatusCode != 200;
}">
<return-response>
<set-status code="@{
var response = context.Variables.GetValueOrDefault<IResponse>("validationResponse", null);
return (int)response.StatusCode;
}" />
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var response = context.Variables.GetValueOrDefault<IResponse>("validationResponse", null);
return response.Body.As<string>();
}</set-body>
</return-response>
</when>
</choose>
<cors>
<allowed-origins>
<origin>*</origin>
</allowed-origins>
<allowed-methods>
<method>GET</method>
<method>POST</method>
</allowed-methods>
<allowed-headers>
<header>Authorization</header>
<header>access-token</header>
<header>Content-Type</header>
</allowed-headers>
</cors>
</inbound>
<backend>
<base />
</backend>
<outbound>
<base />
</outbound>
<on-error>
<base />
</on-error>
</policies>
I tried to use Basic V2 but had same problem. I need to understand with this code have a problem or the version. If this proble is the code, how can I resolve ?
Thks!
Pedro Henrique G Ramalho is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.