Let’s say I have a StackOverflow-esque site. I want to post a comment underneath someone’s answer. Two things can happen:
1) The comment post is successful. I return the actual formatted DIV as some JSON response, set the JSON.
{
"Success": true,
"Data": "some escaped html to inject"
}
2) The comment post is unsuccessful. I return the error as the JSON response.
{
"Success": false,
"Data": "You can only post every 15 seconds"
}
Is this the correct way to do things? I notice that StackOverflow is returning Apache 500 Internal error codes when a comment post is unsuccessful, which I find fishy.
Isn’t a 500 Internal error reserved for things that are actually wrong with the server, not just because some comment validation failed?
I agree that using
500 Internal erroris a little strange, but there are a multitude of applicable response codes you can use to meet your needs.There’s a good list here: http://en.wikipedia.org/wiki/List_of_HTTP_status_codes
Example:
User isn’t logged in, needs to be logged in to comment. Send:
401 UnauthorizedUser is logged in, trying to comment on a closed thread. Send
403 Forbiddenor400 Bad Request.