Mapping exceptions to HTTP status codes in REST web services

When building REST web services in .NET, what is the most "RESTful" way of mapping System.ArgumentNullException and System.ArgumentException to HTTP status codes? My first guess would be to use HTTP 400/Bad Request with an appropriate description.What is the recommended best practice when mapping exceptions to HTTP status codes?

Answers


In general, the 4xx status codes tell the client that the request failed but may succeed if the request i smodified. The 5xx codes inform the client about problems that where the client has no influence.

So the first distinction you have to make is between 4xx and 5xx codes, i.e. tell the client if it should retry or not.

HTTP 400 "Bad Request" should be used if the request was indeed syntactically malformed, incomplete, contradicting or otherwise basically wrong. Additionaly it may be a valid default status in the 4xx range, if no other status seems appropriate and you believe the client needs only to modify the request to succeed.


It depends on the context. E.g. an ArgumentNullException could stem from a violated precondition or be an internal server error.

Regards, tamberg


Need Your Help

Create IMAP account in Outlook 2007 programmatically

c# outlook outlook-object-model

we're rolling out a new voicemail system, and trying to figure out a way to programmatically add a new IMAP account to a user's Outlook.

File reading issues in Nokia Asha 303

java-me nokia-s40

I am reading a fixed number of characters from a file. The file contains both english and arabic text. I have two devices Nokia X3-02 and Nokia Asha 303 for testing. On Nokia X3-02, I am getting the