David
David

Reputation: 1293

Azure API Management - Map Requests to different response codes

I have added an external API to API management. The API always returns 200 response even for bad requests. The response has a property called status which says if the response is ok or if it is a bad request

Can I configure API Management to return a response code based on the 'status' property in this response? Is so how do I do that?

Upvotes: 0

Views: 763

Answers (1)

MKaz
MKaz

Reputation: 770

You can use set-status policy (to set the HTTP status code) together with choose policy (to check your property value). Check set-status documentation for details.

The following example from Microsoft documentation shows how to return a 401 response if the authorization token is invalid.

<choose>
  <when condition="@((bool)((IResponse)context.Variables["tokenstate"]).Body.As<JObject>()["active"] == false)">
    <return-response response-variable-name="existing response variable">
      <set-status code="401" reason="Unauthorized" />
      <set-header name="WWW-Authenticate" exists-action="override">
        <value>Bearer error="invalid_token"</value>
      </set-header>
    </return-response>
  </when>
</choose>

Upvotes: 2

Related Questions