Which http status code to use for no search results found?

Published on July 04, 2020

I was implementing a search REST API and was thinking about the no results status. There are a couple of options that are somewhat valid. There is no perfectly “right” answer and the discussion exposes care for API design, knowledge of http and care for developers who will be consuming the api.

The scenario

Say we are searching some rest api for results GET /api/collection?filter=value&filter=value. What should the API return if there are no results?

The go-to response for a non-error is of course 200 OK but I wanted to think it through instead of defaulting to OK. The analysis depends on HTTP code RFC interpretation and conventions around what is a REST response. It’s subjective.

I still ended up preferring 200.

404 Not Found

Not Found sounds like an option that could work well but the 400 series of response codes are used to indicate that the user request was in error.

This is valid in the case where a single entity requested by the user doesn’t exist. The user asked for a resource that doesn’t exist. They should change their request.

But no search results is not a user error (depending on your api of course). No results is an expected response from a collection api like a search filter.

In this endpoint the resource is the collection itself. The collection should exist. Using a 404 could also lead a developer to think that your endpoint/collection doesn’t actually exist when in fact it’s just that there is no data. This could waste a lot of time.

For an issue where the developer tries to search a collection that doesn’t exist. I would return a 404.

204 No Content

No content also sounds like something that might be appropriate. The search was successful but there are no results. This is semantically correct. However the RFC states that

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

So I don’t like this for the fact that a client has to handle no response body. It’s not the worst choice but it feels wrong.

200 OK

This is a great option. It’s straightforward. The request itself was successful.

I feel that having a consistent array in the body is easier to work with and since we design APIs to be consumed as easily as possible.

A 200 OK is the way to go and I was just over thinking this!

Darragh ORiordan

Hi! I'm Darragh ORiordan.

I live and work in Sydney, Australia building and supporting happy teams that create high quality software for the web.

I also make tools for busy developers! Do you have a new M1 Mac to setup? Have you ever spent a week getting your dev environment just right?

My Universal DevShell tooling will save you 30+ hours of configuring your Windows or Mac dev environment with all the best, modern shell and dev tools.

Get DevShell here: ✨ https://usemiller.dev/dev-shell


Read more articles like this one...

List of article summaries

#engineering

How to add canonical meta tag in NextJs

It’s important to add a canonical meta tag to your pages to improve SEO or to avoid issues with query params in crawled pages.

You can easily add a canonical meta tag in NextJs by using the next/head component.

#engineering

Open Telemetry in NextJs and NestJs

I wrote about how to use open telemetry with NestJs and React previously.

I wanted to add open telemetry to my NextJs app that calls a NestJs backend. The paradigm of SSR preferred by NextJs is a bit different than the CSR paradigm of React.

I’ll describe the differences and how I added open telemetry to NextJs that propagates to other backend APIs.

#engineering

Comparing next start and next standalone with docker

I wrote about how to use nextjs with docker.

I wanted to compare using next standalone like in the article and just using next start.

#engineering

Force RSA key support for Azure DevOps Git SSH

If you’re using Azure DevOps Git SSH you have to use an RSA key. This is because Azure DevOps doesn…

Comments