0

I've got a GET resource that takes a couple of parameters, let's call them CompanyId and CategoryId

This resource can return no results if either the CompanyId or CategoryId are invalid values (Ie: the CompanyId or CategoryId don't exist), or if they're valid, but we just don't have any data for them.

I'd like the client to be able to distinguish between the two cases, so simply returning a an empty 200 or 204 with no data for the former case isn't appropriate.

One way would be just to return a 200 with a message of "Company with Id of x does not exist, but this feels to me like a client error, they've supplied incorrect data.

Is it appropriate to use 422 'Unprocessable Entity' in this instance?

MrBliz
  • 5,830
  • 15
  • 57
  • 81

2 Answers2

0

If your request isn't successful, you shouldn't use a status code 2xx.

I would rather return a status code 400 with a payload describing the problem (query parameter xx is required).

I would use a status 422 if a payload was sent with a method POST or PUT.

Hope it helps you. Thierry

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • 400 feels wrong if the supplied input parameters are in the correct format? – MrBliz Mar 25 '15 at 20:21
  • Yes, agreed! 400 must be used if the parameters are wrong (wrong format or don't exist). Otherwise 200 is correct. 204 doesn't apply since you return an empty list (i.e. [] in JSON format) instead of nothing (i.e. no content)... – Thierry Templier Mar 25 '15 at 21:49
0

The HTTP response codes in the range of 4xx are for error in request, distinct from 5xx which are supposed to be sent after a server error.

Maybe in simple word:, 4xx if client or its request is bad, 500 if something went bad at server side.

400 Bad Request Is the standard and canonical status code for such event.

By default, web servers, web frameworks and also browsers will understand a 400 as Bad request but you should also provide a message in the response body explaining what went bad by the client.

See this Wikipedia article or this by W3C.

diegoaguilar
  • 8,179
  • 14
  • 80
  • 129