Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTTP response codes to distinguish between improper parameters and failing values

Tags:

rest

http

What are suitable response codes and messages for:

  1. fields submitted the wrong way (URL parameters vs body) or missing fields
  2. fields getting invalid values (string instead of numbers, timestamp in future)
  3. some characters like ?, / break stuff in URL parameters
  4. Actual failures: invalid credentials, repeating already-done action

At present, we use 400 for all.

like image 838
Jesvin Jose Avatar asked Jul 04 '12 09:07

Jesvin Jose


People also ask

What are some HTTP response codes What does it mean 2xx 3xx 4xx 5xx?

2xx successful – the request was successfully received, understood, and accepted. 3xx redirection – further action needs to be taken in order to complete the request. 4xx client error – the request contains bad syntax or cannot be fulfilled. 5xx server error – the server failed to fulfil an apparently valid request.

What is the difference between 200 and 201 status code?

Perhaps the most common status code returned is 200. It simply means that the request was received, understood, and is being processed, whereas the 201 status code indicates that a request was successful and a resource was created as a result.

Which HTTP status code is usually returned when a server is overloaded?

503: “The server is unavailable to handle this request right now.” The request cannot be completed at this point in time. This code may be returned by an overloaded server that is unable to handle additional requests.


1 Answers

Cases 1, 2 and 3 in your question are essentially syntactic errors in the request

=> 400 Bad Request

(RFC 2616 says: The request could not be understood by the server due to malformed syntax.)

As to case 4:

a. Invalid credentials

=> 401 Unauthorized

b. Repeating already-done action

=> 403 Forbidden

(The RFC says: The server understood the request, but is refusing to fulfill it. Authorization will not help and the request SHOULD NOT be repeated.)

But then 409 Conflict and 410 Gone make sense when trying to modify stuff incorrectly (PUT) or accessing resources already deleted, respectively.

And here is RFC 2616 Section 10.

like image 119
ArjunShankar Avatar answered Sep 30 '22 11:09

ArjunShankar