Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper HTTP response code for request without mandatory fields

Tags:

Consider simple case where user is deleting a post. This is simple HTTP DELETE/POST request with one mandatory field, post_id.

What should server do if post_id is not provided?

Apparently, user should never encounter this behaviour, so let's be puristic.

My first take would be 400 bad request, but spec says

The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications. 

and I'd say missing field is OK from syntax/http POV, it's application domain-specific semantic requirement.

200 OK with explanations is bad, 500 feels weird as this is request problem.

Thoughs?

like image 424
Almad Avatar asked Feb 22 '11 12:02

Almad


People also ask

What is a 201 response code?

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

Should POST request be returned 200 or 201?

The 200 status code is by far the most common returned. It means, simply, that the request was received and understood and is being processed. A 201 status code indicates that a request was successful and as a result, a resource has been created (for example a new page).

What is a 202 response?

The 202 response is intentionally non-committal. Its purpose is to allow a server to accept a request for some other process (perhaps a batch-oriented process that is only run once per day) without requiring that the user agent's connection to the server persist until the process is completed.

What does HTTP 200 response code specifies?

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.


1 Answers

400 is the correct response.

400 is not restricted to a malformed syntax from an HTTP point of view. Missing a mandatory argument is an error in the syntax defined by the application and thus a "Bad Request"

EDIT

At first it seems strange that there is no separate return code for this, but the return codes are designed to differentiate in what actions the client should take. A 400 error code means that the client should change the POST data or query string to the format defined by the application. Hence it is appropriate for this case.

like image 100
Tomas Avatar answered Oct 11 '22 14:10

Tomas