darkhorse
darkhorse

Reputation: 8722

Correct status code to send for an HTTP POST request where the given ID does not match an object in the database

Lets say I have an endpoint in my API with the following URL:

www.example.com/api/create-order-for-restaurant/

Now, in my POST request, I send the following data:

{
    "restaurant_id": 43,
    "order": {...}
}

Lets say a restaurant with the id 43 does not exist in my database. What should be the appropriate HTTP status code that should be sent back to the client?

Im confused if I should be sending back 404 or 500.

Upvotes: 0

Views: 178

Answers (1)

Evert
Evert

Reputation: 99533

This is a client error because the client specified a restaurant_id that didn't exist.

The default code for any client error is 400, and it would be fitting here too. There are slightly more specific client error codes that might work well for this case, but it's not terribly important unless there is something a client can do with this information.

  • 422 - could be considered correct. The JSON is parsable, it just contains invalid information.
  • 409 - could be considered correct, if the client can create the restaurant with that specific id first. I assume that the server controls id's, so for that reason it's probably not right here.

So 400, 422 is fine. 500 is not. 500 implies that there's a server-side problem, but as far as I can tell you are describing a situation where the client used an incorrect restaurant_id.

Upvotes: 2

Related Questions