Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What HTTP status response code should I use if the request is missing a required parameter?

I am thinking 412 (Precondition Failed) but there may be a better standard?

like image 426
EA. Avatar asked Jun 16 '10 03:06

EA.


People also ask

What is request parameter missing?

It is a Client-side Error which means that either the page has been removed or moved and the URL was not changed accordingly, or that you typed in the URL incorrectly. Its means server is not able to find the URI you specified. but in your case URI is valid but parameters are missing so 400 is right way to do it.

What is a 204 HTTP response code?

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page.

When should I use 200 or 204 status code?

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.

What does HTTP 201 response code specifies?

The HTTP 201 Created success status response code indicates that the request has succeeded and has led to the creation of a resource.


2 Answers

Status 422 seems most appropiate based on the spec.

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

They state that malformed xml is an example of bad syntax (calling for a 400). A malformed query string seems analogous to this, so 400 doesn't seem appropriate for a well-formed query-string which is missing a param.

UPDATE @DavidV correctly points out that this spec is for WebDAV, not core HTTP. But some popular non-WebDAV APIs are using 422 anyway, for lack of a better status code (see this).

like image 94
Kelvin Avatar answered Oct 11 '22 20:10

Kelvin


I'm not sure there's a set standard, but I would have used 400 Bad Request, which the latest HTTP spec (from 2014) documents as follows:

6.5.1. 400 Bad Request

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

like image 37
Gert Grenander Avatar answered Oct 11 '22 21:10

Gert Grenander