What is the correct response for web service?
One of my web ser开发者_StackOverflow社区vices returns the list of ids of the the projects a person is owner of. That is, when I GET http://example.com/wsListProjects?user=eric I get back xml with the list of projects. However, when I specify user=xxx, who doesn't exist, I currently return a 404 response? Is this the correct response to return?
Thanks.
It really is a matter of opinion. Many web services, such as Twitter, often use HTTP return codes when they make sense ( such as 401 Unauthorized if the users credentials are wrong ), others simply return an error object in XML format, while others simply return nothing. It really all comes down to your design.
Is there any difference (from a "program needs to do something" perspective) between a user who doesn't exist and a user who doesn't have any projects assigned? If not, then just return an empty list in either case.
If there is a difference, then perhaps add a MessageCode/MessageText field that's filled out if the user doesn't exist, along with a blank list.
In either case, I don't think a 404 is appropriate - the site exists, and the 404 suggests that something is wrong with the actual web request.
I will return 404 if the user should not have access to the page. Typically you would return 403 (or 401) however in some cases a 404 makes good sense because you want to appear as though the page doesn't even exist. Really depends on the situation.
精彩评论