Set status code

APPLIES TO: All API Management tiers

The set-status policy sets the HTTP status code to the specified value.

Note

Set the policy's elements and child elements in the order provided in the policy statement. Learn more about how to set or edit API Management policies.

Policy statement

<set-status code="HTTP status code" reason="description"/>

Attributes

Attribute Description Required Default
code Integer. The HTTP status code to return. Policy expressions are allowed. Yes N/A
reason String. A description of the reason for returning the status code. Policy expressions are allowed. Yes N/A

Usage

Example

This example shows how to return a 401 response if the authorization token is invalid. For more information, see Using external services from the Azure API Management service.

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

For more information about working with policies, see: