I am implementing a REST API Framework, and I wonder what the recommendedbehavior is, when a client submits an invalid querystring parameter.
I will illustrate what
Since this is a REST call, we are talking about resources. And whenever we have a wrong filter, we should return a proper error code.
In this case i would go for 400 - bad request
as the resource was found and correctly mapped (/api/contacts
), but there was a problem with the query string
part. Therefore a 400
and not a 404
.
Would return a 404
if someone requested /api/contacts-all
or some non-existant resource.
EDIT based on comments below
Agree to your comment. Ideally a 400
is a problem with the request. Going by that, you could use a 422 Unprocessable Entity
. Please look at the stackoverflow link below and it talks about the same thing.
I would guess that developers around the world would be more comfortable seeing a 400
than 422
for such logical errors due to the fact that bigger companies are using 400
and not 422
.
References: Http status codes and 400 for logical error vs malformed request
Following the letter of the law, the response should be a 404 Not found. However, nobody is going to get too upset with you if you prefer to return 400 - bad request.
I would definitely return a 4XX status code though. You want the client to know that they made an error.