The API that I'm working on now throwing 500 Internal Server Error when the request body do not have certain property inside. I want to eliminate that and instead, it returns another status code such as 400 Bad Request. I get that the error is return from APIM from Azure, how can I throw the error inside the set-body policy since I deserialize the request body in here?
I've read many posts and documents but no luck on succeeding that. Here's my code right now:
<inbound>
<send-request mode="new" response-variable-name="" timeout="60 sec">
<set-method>POST</set-method>
<set-header name="Content-Type" exists-action="override">
<value>application/json</value>
</set-header>
<set-body>@{
var request = context.Request.Body.As<JObject>(preserveContent: true);
if(String.IsNullOrEmpty((string)request["id"])){
WHAT SHOULD I PUT HERE?
}
return request.ToString();
}</set-body>
</send-request>
</inbound>
Thanks and appreciate if anyone could help.