im currently working on a website which has Spring at backend and Angularjs at front side and we had discussed about back end responses to handle frontend’s message dialogs and i have a question to ask:
Lets say i have an API :
GET : /getstatistics Request params : fromTime,toTime ( in timestamp format)
And if client make a request with invalid params like a string, which response code should be returned from server ? HTTP 400 bad request and response body with a message ” fromTime and toTime should be in timestamp format” or HTTP 200 with same message?
Any advides and opinions are appreciated.
You should be returning a
error for bad request. Check out this
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).
Please have a look at RFC7231#section-6
A client MUST understand the class of any status code, as indicated by the first digit
4xx (Client Error): The request contains bad syntax
or cannot be fulfilled
Bad syntax can be something like you’ve mentioned in your question (making a request with invalid parameters, like a string).
I keep these two references handy whenever I’m designing RESTful APIs, might be helpful for you too: