Reputation: 1293
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
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