Reputation: 1092
I created an API in Azure API Management and I realized that APIM actually hides some of the bodies I return in responses. I get 200, but I don't get 404, 401, or 500.
I understand that it's better to control what is returned and that we don't want to give the end-user HTML with 500 error body.
But, I use 404 or 401 in my API and I returned some information in the response body. Is there a possibility to control what is being returned for certain status codes?
Upvotes: 1
Views: 2242
Reputation: 20067
Use the set-body
policy to set the message body for incoming and outgoing requests. To access the message body you can use the context.Request.Body
property or the context.Response.Body
, depending on whether the policy is in the inbound or outbound section.
You can custom response body with below code:
<when condition="@(context.Response.StatusCode == 404)">
<return-response>
<set-status code="404" reason="NotFound">
<set-header name="Content-Type">
<value>application/json</value>
</set-header>
<set-body>@{
var response = context.Response.Body.As<JObject>();
foreach (var key in new [] {"HTTP Error 404. The requested resource is not found."}) {
response.Property (key).Remove ();
}
return response.ToString();
}</set-body>
</return-response>
</when>
Upvotes: 1