Reputation: 3157
I'm working on an AJAX based submit of a form which will also be made available as an API for other applications. Therefore I am validating the Request that comes into the Save routine to ensure that fields contain relevant data (numbers in numeric fields, dates in date fields, etc)
I believe it should be best practice to return a HTTP 4xx response code if the user submits nonsense data in the Request but if I set the HTTP Status code to 400 then I get no Response body text.
I've inherited the HTTPException to track specific details of the error which I would like to relay to the client, and the API interaction will all be conducted via JSON.
My code is (roughly):
try
//my request validation code
catch e as myRequestLoadException
response.write("{")
response.write("""errormessage"": """ & e.message.replace("""","\""") & """")
response.write("""errortext"": """ & e.tostring.replace("""","\""") & """")
response.write("}")
Response.StatusCode = e.myStatusCode
end try
The response I get either using Postman or the Chrome developer tools is:
"Bad Request"
If I remove the line where I set the Response Status code and repeat the test then I get the response I am expecting.
So is it IIS, the .net framework or even Chrome witholding the response body?
Upvotes: 2
Views: 1416
Reputation: 56688
It is IIS, which by default does not allow custom content for responses reporting errors. To allow that, make the change in your web.config:
<system.webServer>
<httpErrors existingResponse="PassThrough" />
</system.webServer>
Taken from this article
Upvotes: 6