Proper REST response for empty table?

rest api return null or empty array
empty response body
http response code for empty list
empty response from server code
empty response and no information in the header
trigger request method not valid
empty response http code
http 200 empty response

Let's say you want to get list of users by calling GET to api/users, but currently the table was truncated so there are no users. What is the proper response for this scenario: 404 or 204?

I'd say, neither.

Why not 404 (Not Found) ?

The 404 status code should be reserved for situations, in which a resource is not found. In this case, your resource is a collection of users. This collection exists but it's currently empty. Personally, I'd be very confused as an author of a client for your application if I got a 200 one day and a 404 the next day just because someone happened to remove a couple of users. What am I supposed to do? Is my URL wrong? Did someone change the API and neglect to leave a redirection.

Why not 204 (No Content) ?

Here's an excerpt from the description of the 204 status code by w3c

The server has fulfilled the request but does not need to return an entity-body, and might want to return updated metainformation.

While this may seem reasonable in this case, I think it would also confuse clients. A 204 is supposed to indicate that some operation was executed successfully and no data needs to be returned. This is perfect as a response to a DELETE request or perhaps firing some script that does not need to return data. In case of api/users, you usually expect to receive a representation of your collection of users. Sending a response body one time and not sending it the other time is inconsistent and potentially misleading.

Why I'd use a 200 (OK)

For reasons mentioned above (consistency), I would return a representation of an empty collection. Let's assume you're using XML. A normal response body for a non-empty collection of users could look like this:

<users>
  <user>
    <id>1</id>
    <name>Tom</name>
  </user>
  <user>
    <id>2</id>
    <name>IMB</name>
  </user>
</users>

and if the list is empty, you could just respond with something like this (while still using a 200):

<users/>

Either way, a client receives a response body that follows a certain, well-known format. There's no unnecessary confusion and status code checking. Also, no status code definition is violated. Everybody's happy.

You can do the same with JSON or HTML or whatever format you're using.

RESTful API design. What should I return if there are no rows , But, then I call /v1/get/books and there are no rows in that table. Should I just return an empty structure? [ ] or would it be better a message and an error code ? REST APIs enable you to develop any kind of web application having all possible CRUD (create, retrieve, update, delete) operations. REST guidelines suggest using a specific HTTP method on a particular type of call made to the server (though technically it is possible to violate this guideline, yet it is highly discouraged).

I'd answer one of two codes depending on runtime situation:

404 (Not Found)

This answer is pretty correct if you have no table. Not just empty table but NO USER TABLE. It confirms exact idea - no resource. Further options are to provide more details WHY your table is absent, there is couple of more detailed codes but 404 is pretty good to refer to situation where you really have no table.

200 (OK)

All cases where you have table but it is empty or your request processor filtered out all results. This means 'your request is correct, everything is OK but you do not match any data just because either we have no data or we have no data which matches your request. This should be different from security denial answer. I also vote to return 200 in situation where you have some data and in general you are allowed to access table but have no access to all data which match your request (data was filtered out because of object level security but in general you are allowed to request).

What is the proper REST response code for a valid request but an , Rest api empty response. Proper REST response for empty table?, This is perfect as a response to a DELETE request or perhaps firing some script that does not� Response header Description; ETag: The ETag for the entity. x-ms-request-id: This header uniquely identifies the request that was made and can be used for troubleshooting the request. For more information, see Troubleshooting API Operations. x-ms-version: Indicates the version of the Table service used to execute the request.

If you are expecting list of user object, the best solution is returning an empty list ([]) with 200 OK than using a 404 or a 204 response.

HTTP Methods – REST API Verbs, REST API guidelines suggest to use specific HTTP method on Table of Contents HTTP GET HTTP POST HTTP PUT HTTP DELETE unless the response includes appropriate Cache-Control or Expires header fields. what should return response and status code? is empty [] with 200 status code or 404� The 204 response MUST NOT include a message-body and thus is always terminated by the first empty line after the header fields. 301 (Moved Permanently) The 301 status code indicates that the REST API’s resource model has been significantly redesigned, and a new permanent URI has been assigned to the client’s requested resource.

It must 200 OK with empty list.

Why: Empty table means the table exists but does not have any records.

404 Not Found means requested end point does not exist.

HTTP status code for an empty result, Or, extended to a REST context, "the REST resource you requested doesn't exist. processed properly and successfully, and here's the (empty) result. On list methods a 404 makes me think the API method does not exist. I am creating a REST TO RFC synchronous scenario. A ZBAPI is getting updated at target end, and a response is to be sent to the webservice. I am able to send the request without any issues and also getting a response, but the response is blank and the record is also not created in the Database table of the ECC.

API Design Patterns and Best Practices, An awesome collection of API design patterns and best practices on resources, These resources are mostly specific to RESTful API design. The main 7 http verb or methods � A nice table of when each method should be used � Using HTTP methods in REST A discussion how to handle empty results� Either Return a 200 (OK) status code, and an empty array in the body. Or Return a 204 (NO CONTENT) status code and NO response body. To me, option 2 seems more technically correct and keeping in line with REST and HTTP principles.

HTTP Status Codes, HTTP status codes and how to use them in RESTful API or Web Services. The key for one of the metadata key-value pairs is empty. InsufficientAccountPermissions: Forbidden (403) Read operations are currently disabled. InsufficientAccountPermissions: Forbidden (403) Write operations are not allowed. InsufficientAccountPermissions: Forbidden (403)

JSON:API — Latest Specification (v1.0), This page presents the latest published version of JSON:API, which is currently version 1.0. null for empty to-one relationships. an empty array ( [] ) for empty to -many null is only an appropriate response when the requested URL is one that with a join table, but the JSON:API protocol would be the same in both cases. In the previous section, you used the collection resource to gather data and populate the UI’s HTML table. With Spring Data REST, the _embedded data set is considered a preview of data. While useful for glancing at data, to get headers like ETags, you need to fetch each resource individually.

REST API HTTP Status Codes and Error Messages Reference, The following table lists HTTP response status codes and their meanings: to 400 are either empty or contain an error response in JSON or XML format. not in the proper range (for example, a timeout that parses as an integer but is 0 or� If we're talking about REST, the schema does not matter as long as it's capable of providing the hypermedia controls for the discovery and manipulation of further resources based on the response alone and no other, out-of-band information which none of the formats mentioned by the OP seems to do. – toniedzwiedz Dec 9 '15 at 13:30

Comments
  • I'd respond with 200 and an empty collection (not an empty response body but rather a collection with no elements inside, this will look different depending on the format returned)
  • 404 in this context would probably be better suited for 'table not found'. I'd say return an empty list.
  • Possible duplicate of What is the proper REST response code for a valid request but an empty data?
  • @EJoshuaS It's not. Both questions are mine and very old. They are similar but not duplicates.
  • @EJoshuaS They are obviously not duplicates. This question is about /api/users while that one is about /api/users/1.
  • Definitely agree. And for REST, I would simply send back a status code of 200 with an empty array: [].
  • Makes sense. No need to make it harder. 404 would be confusing.
  • Lets assume API which describe coins in you pocket, with endpoints: GET /singleCoin - returns random single coin from your pocket,GET /severalCoins- returns some coins from your pocket you can grab in one time. Lets say you have no coins in your pocket at now. When you ask to GET /singleCoin you'll get 404 Not Found, but when you ask to GET /severalCoins you'll get 200 OK with empty list []. One fact - you have no coins, described with different responses, why? I whould say it's better always to get 404 Not Found, because there is no coins found in your pocket.
  • @sempasha It depends on what you mean by GET /severalCoins. If you mandate that GET /severalCoins must return some coins then it shouldn’t be 200 because it’s not OK; server failed to provide what client want. For /singleCoin this is obvious because client want exactly one coin, no more, no less. This is same for /coins/7. In contrast for /coins endpoint, typically clients expect no coin, one coin, or multiple coins. All of them are valid response. If there's no coin, then this is what they want. It’s like an emply List<Coin> in Java, instead of null.