What HTTP status code to use for the required parameters is not provided?

I have several pages designed to be called using AJAX - I return their abnormal status code if they cannot be displayed and my javascript will display the corresponding error window.

For example, if the user did not authenticate or their session was not completed, and they try to call one of the AJAX pages, it will return 401 Unathorized .

I also have some 500 Internal Server Error return if something really strange happens on the server side.

What status code should be returned if one of these pages was called without required parameters? (and therefore cannot return any content).

I looked at the wikipedia article on HTTP status codes , but the one closest to the code I was looking for was as follows:

422 Non-process organization
The request was well-formed, but due to semantic errors it could not be completed.

Edit: the above code is specific to WebDAV and therefore unlikely to be appropriate in this case

Can anyone think of an appropriate return code?

+45
Feb 26 '12 at 16:29
source share
5 answers

What status code should be returned if one of these pages was called without required parameters? (and therefore cannot return any content).

You can choose 404 Not Found :

The server did not find anything that matched the Request-URI [if your required parameters are part of the URI, i.e. $_GET ] . It is not indicated whether this condition is temporary or permanent. The status code 410 (Gone) MUST be used if the server, through some mechanism that can be internally configured, knows that the old resource is constantly unavailable and does not have a forwarding address. This status code is usually used when the server does not want to determine exactly why the request was rejected, or when another response is not applicable.

(highlight me)

404 Not Found is a subset of 400 Bad Request that could also be accepted because it is very clear what it is:

The request could not be understood by the server due to incorrect syntax. The client SHOULD NOT repeat the request unchanged.

I canโ€™t actually assume that you are choosing a WEBDAV response code that does not exist for HTTP clients using hypertext, but you could, itโ€™s absolutely true that you are a server encoder, you can actually accept any HTTP response status code you consider necessary for your HTTP client, which you are also a developer:

11.2. 422 Non-process organization

Status code 422 (raw entity) means the server understands the content type of the request object (therefore, 415 (Unsupported media type) is not suitable), and the syntax of the request object is correct (thus, 400 (failed request) status code is not suitable), but not was able to process the contained instructions. For example, this error condition may occur if the XML body of the request contains well-formed (that is, syntactically correct) but semantically erroneous XML instructions.

An IIRC request entity is a request entity. Therefore, if you work with the request authorities, this may be appropriate, as Julian wrote.




You commented:

IMHO, the text for 400 indicates incorrect syntax. I would suggest that the syntax here refers to the syntax of the HTTP string that the client sends to the server.

It can be, but it can be something syntactically expressed, the entire request, only some request headers or a specific request header, request URI, etc. 400. Not specifically about the "HTTP string syntax", it is a general response to a client error:

The 4xx class code class is designed for cases where the client seems to be wrong. Except when responding to a HEAD request, the server SHOULD include an object containing an explanation of the error situation, and whether this is a temporary or permanent condition. These status codes apply to any request method. User agents MUST display any item on the list.

The important role here is that you must tell the client what went wrong. The status code simply says that something went wrong (in class 4xx), but HTTP was not specifically designed to eliminate the missing parameter of the request details and information as an error condition. In fact, the URI only knows that there is a part of the request-information, and not what it means.

If you think 400 is too wide, I suggest you choose 404 if the problem is with a URI, for example. $_GET .

+30
Feb 26 2018-12-12T00:
source

I do not know about the intentions of the authors of the RFC, but the status code that I saw in the wild for this case is 400 Bad Request .

+8
Feb 26 '12 at 16:33
source

422 is a regular HTTP status code; and it is used outside of WebDAV. Contrary to what others say, there is no problem with this; HTTP has a status code registry for some reason.

See http://www.iana.org/assignments/http-status-codes

+6
Feb 26 2018-12-12T00:
source

Description as stated in 400

Request cannot be understood by server due to strong syntax . The client SHOULD NOT repeat the request unchanged.

(Emphasis mine)

This indicates incorrect syntax, which is not the case when the browser sends a request to the server. Its just a case with missing parameters (no distorted syntax yet).

I would advise sticking to 404 :)

(Experts correct me if I'm wrong somewhere :))

+1
Feb 26 2018-12-12T00:
source

Read this carefully:

https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

422 is a WebDAV-specific thing, and I have not seen it used for anything else.

400, although not intended for this specific purpose, seems to be a common choice.

404 is also a viable choice if your API is RESTful or similar (using part of the URI path to specify search parameters)

0
Feb 26 2018-12-12T00:
source



All Articles