10

For example i have an api method /api/orders.getOrders which actually always exists. If this method returns no data in following format, should i send 404 or 200 http response code?

{ "orders":[] }

8
  • 2
    I think you should consider changing your API to GET /api/orders for something more REST friendly then you can return a 200 because the resource exist, even if empty. 404 would mean your resource does not exists ~= url is not correct Commented Jul 29, 2016 at 12:50
  • 204. @RonanQuillevere How can an URI be more or less REST-friendly? Please explain. REST does not put any constraints on the URI design, HTTP also. The resource just has to be uniquely identifiable, hence the URI. So please explain. Commented Jul 29, 2016 at 12:57
  • you should not use verbs in your URLs but instead use the HTTP verbs (GET,POST,PUT,DELETE ...) for that. I would suggest reading this : developer.pearson.com/sites/default/files/… Commented Jul 29, 2016 at 13:45
  • @RonanQuillevere I agree that from an URI design standpoint URIs should not include verbs, but from a REST perspective it is not of relavance as REST is not a protocol but just a style and therefore does not dictate how the URI should look like. We as humans tend to put semantics into certain path segments, from a computer standpoint this is just a further string. Despite the usage of verbs in URIs, a service processing the URI may still be RESTful (depending if it respects the principal REST constraits and also respect the HTTP) Commented Jul 29, 2016 at 15:11
  • uri design is not the point of my question. Commented Jul 29, 2016 at 15:24

1 Answer 1

10

200 is correct.

From RFC 7231

The 4xx (Client Error) class of status code indicates that the client seems to have erred.

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource

In your case, the client did not make a mistake in asking for the resource; the origin server did find a current representation of the resource, so 404 (indeed, the entire 4xx class of responses) is not appropriate.

204 is also wrong.

The 204 (No Content) status code indicates that the server has successfully fulfilled the request and that there is no additional content to send in the response payload body.

"No content" means that the HTTP response message body is empty, which is to say the representation being returned is 0 bytes long. It's not appropriate when returning a non empty representation of an empty resource.

Sign up to request clarification or add additional context in comments.

1 Comment

204 restricts to send an entity body, that is right. Though, on receiving a 204 response, the client already knows that no data for that resource is available. Instead of returning something like { "orders": [] } for a 200 response, 204 and no body is viable too. This furthermore prevents the client from parsing the response body, as there is none.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.