Empty list, HTTP status code 200 vs 204 vs 404 (2024)

Choosing HTTP status codes Series - Part 4
By Arnaud Lauret, June 2, 2021

When designing APIs, choosing HTTP status codes is not always that obvious and prone to errors, I hope this post series will help you to avoid common mistakes and choose an adapted one according to the context.This fourth post answers the following question: given that /users is a collection (a list) and no users are named Spock, what should return GET /users?name=spock? 200 OK, 204 No Content or 404 Not Found

Choosing HTTP status codes Series

When designing APIs, choosing HTTP status codes is not always that obvious and prone to errors, I hope this post series will help you to avoid common mistakes and choose an adapted one according to the context.<div class="alert alert-info">I never remember in which RFCs HTTP status codes are defined.To get a quick access to their documentation, I use Erik Wilde’s Web Concepts.</div>Very special thanks to all Twitter people participating to the #choosehttpstatuscode polls and discussions

  • 1 - This is not the HTTP method you're looking for, HTTP status code 404 vs 405 vs 501
  • 2 - Hands off that resource, HTTP status code 401 vs 403 vs 404
  • 3 - Move along, no resource to see here (truly), HTTP status code 204 vs 403 vs 404 vs 410
  • 4 - Empty list, HTTP status code 200 vs 204 vs 404

When you need to represent lists (aka collection resources) in a REST/RESTful/RESTish API, a usual design pattern is to represented with them a /resources (or /resource, read /resources rules and /resource sucks … or is it the other way around?).More often than not, you’ll need to be able to return a subset of the list’s elements.To do so, a usual (if not standard) design pattern is to add query parameters to provide search filters.If a GET /users is supposed to return a list containing all (actually accessible to the consumer and possibly to the end user) users, a GET /users?name=spock is supposed to return a list containing only the users whose name is spock.The question we will answer today basically is: which HTTP status code respond with when returning an empty list.

Empty list, HTTP status code 200 vs 204 vs 404 (1)

According to my Twitter pool, 51% of respondents would return a 200 OK, while 24% would return a 204 No Content and 25% would return a 404 Not Found.Let’s see what could be the correct answers according to RFC(s) and common practice.

The 200 (OK) status code indicates that the request has succeeded.

Let’s start with the most common and valid response in such a case: 200 OK.When responding to GET /users, the API will respond with that HTTP status code along with a list of all (actually accessible to the consumer and possibly to the end user) users users.When responding to GET /users?name=smith, the API will respond also that with HTTP status code along with a list contains all users named smith.And finally when responding to GET /users?name=spock and if there are no user name spock, the API will respond yet another time with that HTTP status code but this time along with an empty list.That is actually the most common response I have ever seen, probably because most people consider that the /users collection/list resource exists and name query param is used to filter the content of the list.

But there is a more specific HTTP status code that could do the trick too.

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.

While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.It is indeed more often used when responding to a PUT (replace) or a PATCH (partial update), when servers don’t want to bother returning the replaced/updated resource or on a DELETE because there is usually nothing to return after a deletion.But it can be used on a GET too.If the request is valid, has been successfully fulfilled and if there is no additional content to send (which is the case as the returned list would be empty), 204 No Content is perfectly understandable and valid answer.

It’s a valid response but I personally will not use it and do not recommend to use it in that case in my context.Because it is not that common (based on my experience, it’s not absolute truth) and may imply more work.Actually, I try to avoid using specific/uncommon HTTP status when a more generic/common one works too, that usually simplifies consumer’s job and also designer’s one as there are less possible choices and behaviors (I’ll write a post about that).Though a consumer must be able to interpret any 2xx as as success and fallback to treat it as 200 OK, that means there will be no response body, so no empty list.That could easily lead to to possible “null pointer exception” or any equivalent requiring more controls in code.A 200 OK with an empty list can be treated the same as non empty list without thinking about it.Note that, consumer may obviously have to check il the list is empty or not to possibly show a message to end user, but the exact same code will work without that control.

But while simplifying choices, note that using that “simplified HTTP” stance, you’ll loose some “HTTP semantic out of the box”. Indeed tne major argument in favor of 204 No Content is that is allows to check empty search results (especially in logs) vs non empty ones without relying on specific response body’s semantic.That’s quite an interesting feature.Maybe we need more APIs actually fully using HTTP semantic to make this 204 No Content response more common and a no brainer.

So choosing between 200 OK and 204 No Content, depends on you and your context.

The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

I just realized that’s the fourth post in this series and 404 Not Found has been involved in all posts so far.Let’s see what say the HTTP RFCs (with an s) about using it in that use case.

If we look at this status code definition in RFC 7231 and if we consider that /users is the resource used even when doing a GET /users?name=spock, returning that HTTP status code makes no sense at all because the resource /users exists, it’s just that the list it contains may be empty.

But is this definition of a resource identifier (excluding query parameters) is actually the correct one?Section 2 of RFC 7231 states each resource is identified by a Uniform Resource Identifier (URI), as described in Section 2.7 of RFC 7230.That section 2.7 of RFC 7230 says the “query” (what is between the first ? and #) is a part of the resource identifier.If we follow the link (it’s quite a maze!) conducting to complete description of the query, we eventually arrive at Section 3.4 of RFC 3986 that says the query component contains non-hierarchical data that, along with data in the path component (Section 3.3), serves to identify a resource.That basically means that /users?name=spock is a resource identifier, so returning 404 is valid according to HTTP RFCs if we want to say “sorry no resource match the strict identifier provided in your query” or “there is no such a users list containing users named spock”.But using that HTTP status code being valid from a pure HTTP perspective, is it actually a good idea to use it in that use case?

In my humble opinion, based on my experience designing APIs, reading and listening to many API practitioners, analyzing many APIs and doing hundreds of API design reviews, I do not recommend to use it in that case because that would break a common practice.In most REST/RESTful/RESTish APIs, a “resource identifier” is actually the resource path without the query part, that may be wrong when speaking strictly HTTP but that is the current state of common practice.In most APIs, 404 Not Found is strongly attached to “there is nothing for the requested path (excluding query parameters)”.It is returned in case involving /path-that-does-not-exist or /collection/{id that does not exist} (see Choosing HTTP status codes Part 2 - Hands off that resource, HTTP status code 401 vs 403 vs 404 or Choosing HTTP status codes Part 3 - Move along, no resource to see here (truly), HTTP status code 204 vs 403 vs 404 vs 410) but not for empty lists (that’s usually a 2xx Success class).Also, returning a 4xx Client Error Class says that consumer made an error, is that really the case here?I don’t think so, the consumer just provided search filters that don’t match any element in a list.

That’s my reasoned opinion of not using 404 Not Found for empty lists, but if you have valid reasons to use this HTTP status code for this use case, don’t forget to be consistent and provide informative error feedback.Indeed, if we take for granted that GET /users?name=spock returns a 404 Not Found if there are no users named spock.What about GET /users if there are no users at all? It should return the same HTTP status code.And differentiating this it from a more common /path-that-does-not-exist will require to add some information in the response body to explain the actual cause of this response.

The lesson of today is that following the HTTP protocol is one thing but there are sometimes various options with pros and cons and sometimes being overly strict can lead to design that are less easy to understand.The question is not about achieving the most perfect design (regarding HTTP) but just achieve a design that makes sense for most people involved and proposes the best as possible DX.And that D in DX includes developers but also designers.Simple design rules that makes sense for most are a key factor in your APIs success.

Empty list, HTTP status code 200 vs 204 vs 404 (2024)

FAQs

What is the difference between 200 and 204 empty list? ›

The main difference between a 200 and 204 status code is that a 200 status code indicates that the server has successfully processed the request and is returning a response with a message body containing the requested information, whereas a 204 status code indicates that the server has successfully processed the ...

Is 404 or 200 empty? ›

Note that since a 404 means that no results were returned regardless of how many you searched for, a 200 simply means that one or more results have been returned and NOT that every requested resource has been found.

What is the difference between HTTP status code 200 and 204? ›

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. While 200 OK being a valid and the most common answer, returning a 204 No Content could make sense as there is absolutely nothing to return.

What is the difference between HTTP 200 and 404? ›

So you see 404 status for non existent or matching URL. When you directly manually access to “404” page in URL you will get 200 status because you are successfully connected to standard page that has informational content that page that client have tried connect to (kjh) doesn't exist.

Should I delete 204 or 200? ›

A 204 ( No Content ) status code if the action has been enacted and no further information is to be supplied. A 200 ( OK ) status code if the action has been enacted and the response message includes a representation describing the status.

When to use 204 vs 404? ›

When to use HTTP Status Code 204 vs 404? Use a 204 status code when the server successfully processes a request but has no content to return. It's great for actions like form submissions or updates where no new page content is needed. Use a 404 status code when the requested resource is not found on the server.

When to use 204 no content? ›

The HTTP 204 No Content success status response code indicates that a request has succeeded, but that the client doesn't need to navigate away from its current page. This might be used, for example, when implementing "save and continue editing" functionality for a wiki site.

Is 404 an empty state? ›

Most UX professionals don't necessarly view 404 pages as empty states, but there are reasons to approach them as such. After all, the data that was supposed to be displayed isn't there. Copy on 404 pages can be (and should be) functional, playful, and goal-oriented.

When to return 404 in rest API? ›

It is common for APIs to also return a 404 in some authorization error cases. The authorization case where a 404 applies is when the client is not allowed to know that the resource exists.

What is the 204 status code used for? ›

HTTP response status code 204 No Content is returned by the server to indicate that a HTTP request has been successfully completed, and there is no message body.

Can 204 response have body? ›

The 204 response MUST NOT include a message body and thus is always terminated by the first empty line after the header fields.

What is the HTTP code 200 indicate? ›

The HTTP 200 OK success status response code indicates that the request has succeeded. A 200 response is cacheable by default. The meaning of a success depends on the HTTP request method: GET : The resource has been fetched and is transmitted in the message body.

What is the most common error 404? ›

A 404 error happens when you try to access a resource on a web server (usually a web page) that doesn't exist. Some reasons for this can be a broken link, a mistyped URL, or that the webmaster has moved the requested page somewhere else (or deleted it).

What is the difference between HTTP 200 and 403? ›

The 200 HTTP error code means that the request you made was successful. This is the code you'll see most often, as it's the code that is returned when your browser successfully connects to a website. 403: The 403 HTTP error code means that the request you made was forbidden.

What is HTTP response 200 without body? ›

200 means success and should never have an empty body. Empty body success responses are supposed to use code 204 – http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html. An empty body is an invalid JSON response, this not being allowed. If it is an authentication failure, this MUST return a 401 or 403 code.

Can 200 have an empty body? ›

200 means success and should never have an empty body. Empty body success responses are supposed to use code 204 – http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html.

What is the 200 status code? ›

An HTTP response status code 200 means success. The client has requested documents from the server. The server has replied to the client and given the client the documents.

Is it really better to return an empty list instead of null? ›

Returning empty lists or arrays instead of null can help you write code that's safer and easier to understand. It eliminates the risk of NullReferenceException when the returned value is used and makes it clear that the method didn't find anything to return.

Top Articles
THE TOP 5 STATES WITH THE MOST SUPERCARS
Mad for the Maldives? Check out these resorts for all budgets
Will Byers X Male Reader
Global Foods Trading GmbH, Biebesheim a. Rhein
Palm Coast Permits Online
Chris Provost Daughter Addie
Kathleen Hixson Leaked
Faridpur Govt. Girls' High School, Faridpur Test Examination—2023; English : Paper II
Summit County Juvenile Court
Professor Qwertyson
Waive Upgrade Fee
The Wicked Lady | Rotten Tomatoes
Ella Eats
Craigslist Boats For Sale Seattle
Miss America Voy Forum
Craigslist Pets Longview Tx
What is Rumba and How to Dance the Rumba Basic — Duet Dance Studio Chicago | Ballroom Dance in Chicago
Craiglist Tulsa Ok
Classic | Cyclone RakeAmerica's #1 Lawn and Leaf Vacuum
Red Devil 9664D Snowblower Manual
Full Standard Operating Guideline Manual | Springfield, MO
Saritaprivate
Zack Fairhurst Snapchat
Hdmovie2 Sbs
Hewn New Bedford
Craigslist Roseburg Oregon Free Stuff
Sadie Sink Reveals She Struggles With Imposter Syndrome
Haunted Mansion Showtimes Near Epic Theatres Of West Volusia
Asteroid City Showtimes Near Violet Crown Charlottesville
Defending The Broken Isles
Обзор Joxi: Что это такое? Отзывы, аналоги, сайт и инструкции | APS
Egusd Lunch Menu
Kqelwaob
Ups Drop Off Newton Ks
Bad Business Private Server Commands
6465319333
NIST Special Publication (SP) 800-37 Rev. 2 (Withdrawn), Risk Management Framework for Information Systems and Organizations: A System Life Cycle Approach for Security and Privacy
Shaman's Path Puzzle
Ducky Mcshweeney's Reviews
Movies123.Pick
Omnistorm Necro Diablo 4
Tds Wifi Outage
Snohomish Hairmasters
Henry County Illuminate
Compare Plans and Pricing - MEGA
San Bernardino Pick A Part Inventory
2Nd Corinthians 5 Nlt
Borat: An Iconic Character Who Became More than Just a Film
Scott Surratt Salary
Craigslist Sparta Nj
Home | General Store and Gas Station | Cressman's General Store | California
683 Job Calls
Latest Posts
Article information

Author: Margart Wisoky

Last Updated:

Views: 6117

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Margart Wisoky

Birthday: 1993-05-13

Address: 2113 Abernathy Knoll, New Tamerafurt, CT 66893-2169

Phone: +25815234346805

Job: Central Developer

Hobby: Machining, Pottery, Rafting, Cosplaying, Jogging, Taekwondo, Scouting

Introduction: My name is Margart Wisoky, I am a gorgeous, shiny, successful, beautiful, adventurous, excited, pleasant person who loves writing and wants to share my knowledge and understanding with you.