9

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":[] }

Outofdate
  • 635
  • 5
  • 16
  • 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 – Ronan Quillevere Jul 29 '16 at 12:50
  • [204](https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.2.5). @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. – Roman Vottner Jul 29 '16 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 : http://developer.pearson.com/sites/default/files/api-design-ebook-2012-03%20(1)_0.pdf – Ronan Quillevere Jul 29 '16 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) – Roman Vottner Jul 29 '16 at 15:11
  • uri design is not the point of my question. – Outofdate Jul 29 '16 at 15:24
  • Have a look at [this answer](http://stackoverflow.com/a/34945989/1426227). – cassiomolin Jul 29 '16 at 15:27
  • That is why this is a comment and I said "you should CONSIDER". This is just a suggestion on my side. @Roman, I agree but if your API is to be used by another developer then it is probably better to design it the way all other public APIs are designed. – Ronan Quillevere Jul 29 '16 at 16:02
  • Does this answer your question? [What is the proper REST response code for a valid request but an empty data?](https://stackoverflow.com/questions/11746894/what-is-the-proper-rest-response-code-for-a-valid-request-but-an-empty-data) – Piotr Dobrogost Nov 23 '20 at 11:57

1 Answers1

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.

Community
  • 1
  • 1
VoiceOfUnreason
  • 52,766
  • 5
  • 49
  • 91
  • 3
    `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. – Roman Vottner Jul 29 '16 at 13:23