Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mapping exceptions to HTTP status codes in REST web services

Tags:

rest

http

wcf

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?

like image 465
Enrico Campidoglio Avatar asked Nov 20 '08 11:11

Enrico Campidoglio


2 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.

like image 169
mkoeller Avatar answered Nov 03 '22 00:11

mkoeller


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

Regards, tamberg

like image 45
tamberg Avatar answered Nov 02 '22 23:11

tamberg