What HTTP code should be returned from the REST API?

im currently working on a website that has Spring on the backend and Angularjs on the front side, and we discussed the answers to the final answers for handling dialog boxes with an external interface, and I have a question:

Let's say I have an API:

GET : /getstatistics
Request params : fromTime,toTime ( in timestamp format)

And if the client makes a request with invalid parameters, such as a string, what response code should be returned from the server? The body of the HTTP 400 request and response with the message "fromTime and toTime should be in timestamp format" or HTTP 200 with the same message?

I saw some Google APIs, for example Oauth, they return 200 code for a request with an invalid access_token, but in our project my opinion should be HTTP 400 , because Javascript has successful callbacks and errors, is it better for it just to pop up a dialog a red window with a message inside and not with an HTTP 200 code, and then you need to check the contents of the message?

Any advice and opinions are welcome.

Thank!

+4
source share
3 answers

You must return an error 400for a bad request. Check out this link.

- - (, , ).

, RFC7231#section-6

,

4xx ( ):

- , ( , ).

, API RESTful, :

+9

, , http- 400 . , 400 422. SO 400 422 POST

+2

, - , . , 404. , 409 . 100% - / .

HTTP "409 Conflict" , .

: w3.org/Protocols/

Edit: In any case, status code 200 is incorrect here because there is an error. In response, you can return certain information as follows:

{
  "errors": [
   {
    "userMessage": "Sorry, the parameter xxx is not valid",
    "internalMessage": "Invalid Time",
    "code": 34,
    "more info": "http://localhost/"
   }
  ]
} 
0
source

Source: https://habr.com/ru/post/1659265/


All Articles