Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Status code to return on not found sub-resource

So let's say I have two resources, Wallet and User. The User and Wallet have a one-to-one relationship. In my REST API, I give the option to give the User a different Wallet, by ID. So a typical HTTP PUT request to move the user to a different wallet could look like this:

PUT /api/user/3 HTTP/1.1
Host: api.myuserandwalletwebsite.com

{
    "wallet_id": 15
}

This will update the User to use the wallet with id=15. But, what if the PUT request contains a wallet_id that is not found in the database; what should the REST API then return? Just a simple 404?

Returning a 404 on a sub-resource not found feels weird to me, because the 404 would be misleading: you could think the 404 actually refers to the user not being found.

like image 987
jaapz Avatar asked Aug 11 '14 09:08

jaapz


People also ask

Is it correct to return 404 when a rest resource is not found?

Something goes wrong, the resource you need to be there is not found. If it would be an endpoint to fetch the list of products that expired yesterday (for instance) but there are none, then a 404 would not be correct since you cannot expect to always have products expire every day.

What is HTTP status code1?

We tend to get -1 status codes when there are network issues or connection problems, so we display the user a network problems page in those cases.

What is HTTP code for resource not found?

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.

How do I find Httpstatus code?

In the main window of the program, enter your website homepage URL and click on the 'Start' button. As soon as crawling is complete, you will have all status codes in the corresponding table column. Pages with the 4xx and 5xx HTTP status codes will be gathered into special issue reports.


Video Answer


2 Answers

404 (Not Found) is definitely not the correct response code. The response code you want is 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.
    -- RFC 4918

It is a well-understood and well-defined response code, and can be found in the Hypertext Transfer Protocol (HTTP) Status Code Registry maintained by IANA.

As a side note, you're not using HTTP PUT according to the spec. A PUT should update the entire contents of the resource in an idempotent manner. You should be using either PATCH or POST.

As an alternative, you might consider a joining resource, such as a /user-wallet endpoint. That may or may not make sense depending on the specifics of your API.

like image 187
Eric Stein Avatar answered Oct 12 '22 23:10

Eric Stein


Like you already mentioned: I would also not use 404, because it would mean the user could not be found. As you want to update your User resource and this update fails due to invalid data (a reference to an invalid Wallet) I would rather use 400 Bad Request and add a meaningful message to an additional header field (e.g. X-Message: Wallet with ID 15 not found).

HTTP 422 Unprocessable Entity is also a good idea, but not covered in RFC2616. If this "extension" is no problem you can go with this one.

HTTP 405 is not correct here as this would mean the PUT method is generally not allowed on the users resource and that PUT would not be included when firing OPTIONS to the resource which is not the case.

like image 37
Sebastian Daschner Avatar answered Oct 13 '22 01:10

Sebastian Daschner