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

http status codes
http response
http 422
400 bad request rest api
error code 420
mdn 404
422 unprocessable entity json
html status

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

404 Not Found is a problematic status to return for a POST request. It implies the resource you are sending the request to doesn't exist; the caller got the URL wrong.

The most obvious (and generic) answer is: 400 Bad Request

This just indicates there is something wrong with your request (the fault lies with the caller not the server) and then express the specific detail of what went wrong in your response body. This is typically how request validation is handled.

The ideal answer is to make it so you add a user by sending a request to the company they are a member of:

POST /company/34
Content-Type: application/json
    "username": "newuser",
    "age": 99

This means the caller has to find a valid company resource to send the request to. If company/34 doesn't exist, a 404 Not Found response is appropriate; you tried adding a user to a company which does not exist.

This does mean your API has to be structured with resource semantics and a user has to belong to exactly one company.

HTTP response status codes, The below status codes are defined by section 10 of RFC 2616. This is typically the response sent after POST requests, or some PUT In an API, this can also mean that the endpoint is valid but the resource itself does not exist. 502 Bad Gateway: This error response means that the server, while  The 404 status code, or a Not Found error, means that the user is able to communicate with the server but it is unable to locate the requested file or resource. 404 errors can occur in a large variety of situations.

A Complete Guide and List of HTTP Status Codes (2020), HTTP status codes are like short notes from a server that get tacked onto a web page. While status codes are returned every single time your browser requests a web Here's a code that explains what went wrong.” This code means that the requested resource does not exist, and the server does not  POST user/{pimary_key} Paylod: - favorite hamburger type This situation creates the potential for several requests from the client that are invalid: CONFLICT - The client updates an existing user attempting to change the username to a value that is already in use by a different user.

Here, this picture is very good, and I've used it many times.

Which code should I return?

I'd go with 404. The resource could exist (not a format error) but it just doesn't (and hence can't be found).

When to use HTTP status code 404 in an API, Reviewing the W3C definitions for HTTP Status codes, gives us this: 4xx codes Tell a UA it did something wrong, the request it constructed isn't proper You use 404 if the resource is not found, not when there's no entity. protocol that format encodes) that no actual data matching the query was found. A Complete Guide and List of HTTP Status Codes. While there are over 40 different server status codes, you’ll likely encounter fewer than a dozen on a regular basis. Below, we’ve covered the more common ones, as well as a few of the more obscure codes you may still run across.

API Status & Error Codes, The Fusioo API attempts to return appropriate HTTP status codes for every request. The URI requested is invalid or the resource requested, does not exists. "code": 10002, "message": "The requested resource was not found", "​data": {} } the incorrect HTTP method is used, for example using GET instead of POST . The 424 (Failed Dependency) status code means that the method could not be performed on the resource because the requested action depended on another action and that action failed. For example, if a command in a PROPPATCH method fails, then, at minimum, the rest of the commands will also fail with 424 (Failed Dependency).

API Responses, PayPal API calls return HTTP status codes. Use these links to request more information about and construct an API flow that is relative For successful requests, PayPal returns HTTP 2XX status codes. call at least once, with the same PayPal-Request-Id HTTP header as before. The specified resource does not exist. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 to hide the existence of a resource from an unauthorized client. This response code is probably the most famous one due to its frequent occurrence on the web.

API Error Codes | HTTP Error Codes, The following is a list of Intervals project management softwares API error codes that may be returned by your request, along with their associated HTTP codes: 14, Resource requested does not exist (e.g. if you wanted task 25 and it doesn't HTTP method not available (e.g. if you try to call DELETE client without the id). The target resource is invalid because it does not exist, Azure AD can't find it, or it's not correctly configured. This indicates the resource, if it exists, has not been configured in the tenant. The application can prompt the user with instruction for installing the application and adding it to Azure AD.

  • I would go for 422 instead, as described in my answer.
  • What about this status connected to WebDAV? I have seen somewhere you should use it only if you support WebDAV capabilities.
  • @AgataAndrzejewska Where is somewhere? WebDAV is a HTTP extensions and 422 is a valid HTTP status code registered in IANA.
  • I generally advise people to avoid using the status codes associated with WebDAV unless they're actively using the WebDAV standard as the basis for their API; I think it's confusing to consumers to cherry-pick a specific (although appropriate) status code from an obscure standard, ignoring everything else about that standard.
  • CassioMazzochiMolin, I have read it in this article:
  • @PaulTurner I was thinking about simplicity and clarity and 400 with a description 'invalid/incorrect company id'. The request just does not seem to have an invalid syntax problem. Generally the POST request I described seems to be not a rare case and I don't see many 422 errors returned in APIs.
  • Thank you, according to this picture I should use 422.
  • 404 makes no sense here.
  • @CassioMazzochiMolin Would you mind trying to add something useful instead of just bashing senselessly, like why my reasoning was wrong?