Enrico Campidoglio
Enrico Campidoglio

Reputation: 59923

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?

Upvotes: 6

Views: 7375

Answers (2)

mkoeller
mkoeller

Reputation: 4477

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.

Upvotes: 11

tamberg
tamberg

Reputation: 2017

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

Regards, tamberg

Upvotes: 1

Related Questions