Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP status while POST with incorrect data (using id of resource which does not exist)

What would be the correct HTTP status to return when I am performing the POST request to create a new user, but one of its parameters is incorrect - the company id I am including with the user data doesn't exist in the database.

POST data: {username: 'newuser', age: 99, company_id: 34}

the company with id 34 does not exist in the database.

I was thinking whether that could be:

  • 400, kind of invalid data, but it is valid but nonexistent id
  • 404 - but it is not so clear which resource does not exist
  • 409, because it is kind of conflict and the user can resolve that by changing the company id
  • 422?
  • or 500 - because it is kind of database error while non existing id's are not allowed there
like image 788
Talita Avatar asked Jun 01 '18 12:06

Talita


People also ask

What is the HTTP status for resource that doesn't exist?

404: “The requested resource was not found.” This is the most common error message of them all. This code means that the requested resource does not exist, and the server does not know if it ever existed.

What happens when you send PUT request but the resource does not exist?

If the target resource does not have a current representation and the PUT successfully creates one, then the origin server MUST inform the user agent by sending a 201 (Created) response.

What is the HTTP status code for data not found?

404 Not Found: This response code occurs when the server cannot find the resources being requested by the client.

What is the HTTP error response code for when a resource is not found?

The HTTP 404 Not Found response status code indicates that the server cannot find the requested resource.


1 Answers

400 or 422

First of all, keep in min that it's a client error, so 5xx status codes are not suitable here. You should pick a 4xx status code then.

The most obvious options are 400 and 422:

  • If the JSON is syntactically invalid, return 400.
  • If JSON is syntactically valid but its content is invalid, return 422 to indicate that the request entity cannot be processed by the server.

See the following quote from the RFC 4918 (for your situation, just read JSON when it says XML):

11.2. 422 Unprocessable Entity

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.

A similar situation was addressed in this answer.


For example purposes, the GitHub API v3 also returns 422 if the content of the payload contains invalid values (but is syntactically valid):

There are three possible types of client errors on API calls that receive request bodies:

  1. Sending invalid JSON will result in a 400 Bad Request response. [...]

  2. Sending the wrong type of JSON values will result in a 400 Bad Request response. [...]

  3. Sending invalid fields will result in a 422 Unprocessable Entity response. [...]


Michael Kropat put together a set of diagrams that's pretty insightful when it comes to picking the most suitable status code. See the following diagram for 4xx status codes:

Picking the right 4xx status code

like image 193
cassiomolin Avatar answered Sep 29 '22 02:09

cassiomolin