Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP status code 4xx vs 5xx

I am creating a REST API and find it difficult to choose the right HTTP status code to return in some cases.

Let's say I expect a certain value, and when it is not present I cannot perform a certain task and return an error. Because of the missing value the server cannot handle the request, but it was the client who sent it, wellformed but incomplete, in. Would it be best to return a 4xx or a 5xx error?

like image 814
g_uint Avatar asked Sep 22 '16 10:09

g_uint


People also ask

What's the difference between 4xx and 5xx HTTP status code?

A 4xx code indicates an error caused by the user, whereas 5xx codes tell the client that they did everything correctly and it's the server itself who caused the problem.

What is status code 4xx?

A 4XX Error is an error that often occurs when a webpage doesn't exist or has restricted access or rights. These type of errors often occur from misspelling the URL. As a result, the page cannot be found or, the site or page could not be reached.

What does status code 5xx series stand for?

What is a 5xx Server Error? A server returns a 5xx server error when it can't successfully complete a client's request. 5xx means any HTTP status code that starts with 5 , so among others, this covers the 500 , 502 , and 503 errors.

What is the category of 5xx HTTP?

5xx - Server Error This group of HTTP status codes indicates that the server is aware that it is on error or is incapable of performing the request. The server response usually includes an explanation of the error situation and if it is a temporary or permanent condition.


1 Answers

Stick to the standards!

The decision of which HTTP status code you will send to the client is up to you but you really should stick to the standards. The RFC 7231 is the current reference for content and semantics of the HTTP/1.1 protocol. It's a must read when creating an API on the top of the HTTP protocol.

4xx vs 5xx status codes

Use 4xx status codes for client errors and 5xx status codes for server errors:

6.5. Client Error 4xx

The 4xx (Client Error) class of status code indicates that the client seems to have erred. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition. These status codes are applicable to any request method. User agents SHOULD display any included representation to the user.

6.6. Server Error 5xx

The 5xx (Server Error) class of status code indicates that the server is aware that it has erred or is incapable of performing the requested method. Except when responding to a HEAD request, the server SHOULD send a representation containing an explanation of the error situation, and whether it is a temporary or permanent condition. A user agent SHOULD display any included representation to the user. These response codes are applicable to any request method.

Which status code you should use

For the situation you mentioned in your question, you could use 400 or maybe 422 (from WebDAV, a HTTP extension):

6.5.1. 400 Bad Request

The 400 (Bad Request) status code indicates that the server cannot or will not process the request due to something that is perceived to be a client error (e.g., malformed request syntax, invalid request message framing, or deceptive request routing).

11.2. 422 Unprocessable Entity

The 422 (Unprocessable Entity) status code means the server understands the content type of the request entity (hence a 415(Unsupported Media Type) status code is inappropriate), and the syntax of the request entity is correct (thus a 400 (Bad Request) status code is inappropriate) but was unable to process the contained instructions. For example, this error condition may occur if an XML request body contains well-formed (i.e., syntactically correct), but semantically erroneous, XML instructions.

Along with the status code, ensure you send a representation (such as JSON or XML) containing an explanation of the error situation in the response payload. Have a look at the RFC 7807, it describes a stardard for problem details for HTTP APIs.

A great decision chart

For more details, check this decision chart from Racksburg:


The status codes are grouped into three rough categories:

HTTP status codes categories


Start here:

HTTP status codes


Choosing 2xx and 3xx status codes:

HTTP 2xx and 3xx status codes


Choosing 4xx status codes:

HTTP 4xx status codes


Choosing 5xx status codes:

HTTP 5xx status codes

like image 140
cassiomolin Avatar answered Sep 26 '22 11:09

cassiomolin