What HTTP status should I use for when a parameter is missing from POST?

23

I have a form that will be submitted to the server via ajax, using a plugin for this. When the form is not filled correctly (in case when missing a mandatory parameter), I want to return a JSON with an error message and an error status, so that in javascript, the plugin goes into error callback rather than success.

I know it must be one of the 4xx family, but I do not know which one.

It does not seem to be any of these:

    400 - Bad request: the request worked out so much that I returned a JSON

  • 404 - Not found: the resource exists so much that I can give POST to it

  • 403 - Forbidden: I do not think so either

What status should I return?

    
asked by anonymous 28.02.2014 / 22:16

3 answers

21

The 422 is the closest to it (I'll tell you more about it).

In 400 , it is spoken about " bad syntax ", but this is not the case (syntax error).

. But that is also not the case, because it is the server (not the requester ) who will actually do the validation. Although, of course, there may be some validation also on the form / client side.

In 412 , it talks about semantic errors. That is, it has no syntax error, but has some missing information. It seems to be the most convenient.

    
01.03.2014 / 01:44
15

Opinions are divided. There is an equivalent question in Stack Overflow in English, where people divide between codes 400, 412 and 422:

link

In this case, you would have to evaluate the context in which the code will be returned to decide which code to return.

    
28.02.2014 / 22:26
7

Very interesting the stack pointed out by @Cigano.

Particularly I support the use of the 400 because the server would be responding that it received a bad request from the client.

But why a bad request?

Because something he needs to provide the ideal and complete answer was absent. And this something can be a URL argument (such as a token), a control field usually stored in hidden , and so on.     

14.08.2014 / 01:26