Status Codes for Web Services
Thursday, July 2. 2009
I gather most js libraries will now provide an error handler for 4xx and 5xx, which is quite handy to know whether to display an error dialog. I also use 4xx for when client errors eg when a db constraint fails, I respond with a 4xx and put the error message in the body of the response. For 5xx I log the error message rather than include it in the response.
There is one YUI component however, yui-uploader, which doesn’t seem to treat 4xx as an error on FF for Windows (but it did on a Mac)
A favourite status code? If only we could respond to our managers with status codes when they ask for unreasonable things or don’t give us the full details, any of the 4xx codes would be my favourite ;)
shangxiao: my manager is geeky enough that he’d know perfectly well what I was talking about if I used a 4xx code at him :)
I don’t use errors codes at all.
Custom error pages with description what is happened and recommendation what user need to do – it is my way.
Keep in mind not all users can understood error by number and less of them will know what to do.
Using a HTTP Error code doesn’t stop you serving up a custom error page. In fact thats the best way to do it.
People who visit with a web browser will be none the wiser, but anyone that needs to programatically find out what the server responded with can. And if you’re serving up an API error page by returning HTTP status 200, expect people like me to hunt you down and slap you. Hard. Because I check the status code to find out if I hit an error in the API or not, as you’re supposed to. Its a standard for a reason.
I wish there were some more 4xx error codes. They are so arcane and not really fit with today’s use of HTTP as an application protocol.
I would love for some extra codes like missing request parameter, parameter validation failed, etc…
Alexandr: I like to use both – return a status code so anything that cares can see it, then make the body of the thing a sensible error message.
Caius: You are providing us good and sensible advice, thanks for dropping by :)
lifeforms: I have no idea how the codes get decided upon but I do see your point. I end up returning a general error code with a meaningful message usually – the advantage of this is that I can stack error messages and return lots of feedback to the user in one go
For authentication problems, I prefer to use a 412 Precondition Failed. For most of my APIs, I assume that the user is logged in (the Precondition). Although it would be more semantically correct to send the Unauthorized header, I prefer to avoid it since browsers will normally handle it by asking a user to login. Which is confusing and impossible, since my web server is definitely not set up to handle it.
Chris: That’s a good point – it depends what you expect to be consuming your service, sometimes browsers "help" us in the least helpful ways!
Roy Fielding, who coined the term "Rest", suggests using 422 for validation errors: http://tech.groups.yahoo.com/group/rest-discuss/message/6183
I like to verify my pages status code and page info such headers, cookies, with this free tool: http://soft-net.net/SendHTTPTool.aspx
It free and it give you a lot of information about the response from the page.



Over the summer months I wrote a series of posts about designing APIs in general, and web services in particular. This included posts on status codes for web services, error feedback for web services, auth mechanisms for web services, saving state in web
Tracked: Oct 28, 08:51