What's the most appropriate HTTP status code f

2019-01-22 10:43发布

I'm curious what's the most appropriate HTTP status code for an "item does not exist" page.

If the page itself doesn't exist, I'll obviously use 404. However, one of my pages has a userid argument (it's an "edit user" page) and in case no user with the given user ID exists I'm displaying an error page, but I'd also like to send a 4xx status header (since "200 OK" doesn't really fit).

I guess 404 would be ok since it's "not found" and not "file not found", but I wonder if there's a better code for this case.

4条回答
兄弟一词,经得起流年.
3楼-- · 2019-01-22 10:57

That's depending if userid is a resource identifier or additional parameter. If it is then it's ok to return 404 if not you might return other code like

400 (bad request) ‐ indicates a bad request
or
412 (Precondition Failed) e.g. conflict by performing conditional update

More info in free InfoQ Explores: REST book.

查看更多
【Aperson】
4楼-- · 2019-01-22 10:59

Getting overly clever with obscure-er HTTP error codes is a bad idea. Browsers sometimes react in unhelpful ways that obfuscate the situation. Stick with 404.

查看更多
地球回转人心会变
5楼-- · 2019-01-22 10:59

A 404 return code actually means 'resource not found', and applies to any entity for which a request was made but not satisfied. So it works equally-well for pages, subsections of pages, and any item that exists on the page which has a specific request to be rendered.

So 404 is the right code to use in this scenario. Note that it doesn't apply to 'server not found', which is a different situation in which a request was issued but not answered at all, as opposed to answered but without the resource requested.

查看更多
登录 后发表回答