What is the proper response status for a REST API GET returning no content?

returned a response status of 204 no content
web api return status code with message
rest api response
http status codes
204 vs 404
204 no content postman
rest http status codes best practices
http 204 vs 200

I have an endpoint like so:

GET /api/customer/primary

If a primary customer exists, I return something like

{
  name: "customerName"
}

But what if I send a GET and a primary customer doesn't exist?

Is it better to send a 200 OK with an empty JSON {}

Or is better to only send a 204 No Content?

404 is the appropriate status code for this. You're trying to represent a 'primary customer' as a resource, but in some cases this relationship doesn't exists. This situation is pretty clear, it should be a 404 for GET requests.

This is a perfectly acceptable way to communicate this. A 404 might signal a client that the resource doesn't exist yet, and perhaps that it can be created with PUT.

204 No Content has a specific meaning, and doesn't make that much sense for your case. 204 is not just meant to signal there's not going to be response body (Content-Length: 0 can do that), but it has a more specific application for hypermedia applications. Specifically, it signals that when a user performs an action that results in the 204, the view shouldn't refresh. This makes sense for for example an "Update" operation where a user can occasionally save their progress while working on a document. Contrast to 205 Reset Content, which signals that the 'view' should reset so (perhaps) a new document can be created from scratch.

Most applications don't go this far. Frankly, I haven't seen a single one. Given that, returning 200 with Content-Length: 0 or 204 No Content is an almost completely irrelevant discussion. The HTTP specification certainly doesn't forbid 200 OK with Content-Length: 0.

That was a bit of a tangent. To conclude, 404 signals this 'thing' doesn't exist, and that's appropriate here. There's no multiple interpretations. There's the people who wrote the specifications, those who read them well and on the other side of the discussion the people who are wrong.

HTTP Response Status Codes – REST API Tutorial, The HTTP Status 200 (OK) status code indicates that the request has been GET, An entity corresponding to the requested resource is sent in the response. any payload in response, then it should send HTTP status 204 (No Content) instead. Reply. Jim. 8 months ago. Do you have to manually return a 200 response  By default, 204 (No Content) response is cacheable. If caching needs to be overridden then response must include cache respective cache headers. For example, you may want to return status 204 (No Content) in UPDATE operations where request payload is large enough not to transport back and forth.

But what if I send a GET and a primary customer doesn't exist?

Is it better to send a 200 OK with an empty JSON {}

Or is better to only send a 204 No Content?

If I'm interpreting your question correctly, you aren't really asking about status codes, but rather what kind of schema should you be using to manage the different cases in your API.

For cases like REST, where the two ends of the conversation are not necessarily controlled by the same organization and same release cycle, you may need to consider that one side of the conversation is using a more recent schema version than the other.

So how is that going to be possible? The best treatments I have seen focus on designing schema for extension - new fields are optional, and have documented semantics for how they should be understood if a field is absent.

From that perspective

{}

Doesn't look like a representation of a missing object - it looks like a representation of an object with default values for all of the optional fields.

It might be that what you want is something like Maybe or Option - where instead of promising to send back an object or not, you are promising to send back a collection of zero or one object. Collections I would normally expected to be represented in JSON as a array, rather than an object.

[]

Now, with that idea in pocket, I think it's reasonable to decide that you are returning a representation of a Maybe, where the representation of None is zero bytes long, and the representation of Some(object) is the JSON representation of the object.

So in that design 204 when returning None makes a lot of sense, and you can promise that if a successful response returns a body, that there is really something there.

There's a trade off here - the list form allows consumers to always parse the data, but they have to do that even when a None is sent. On the other hand, using the empty representation for None saves a parse, but requires that the consumer be paying attention to the content length.

So, looking back to your two proposals, I would expect that using 204 is going to be the more successful long term approach.

Another possibility would be to return the null primitive type when you want to express that there is no object available. This would go with a 200 response, because the content length would be four bytes long.

null

HTTP Status 200 – OK – REST API Tutorial, 404 is the appropriate status code for this. You're trying to represent a 'primary customer' as a resource, but in some cases this relationship  Rest API request and response bodies will change over time. You want to be able to make additive changes to your REST API. For that reason you should never return a JASON array as the root type for your REST response, always an object; even for bulk / list requests.

What is the proper response status for a REST API GET returning no , The HTTP 204 No Content success status response code indicates that The common use case is to return 204 as a result of a PUT request,  To prevent an API consumer from having to hit the API again for an updated representation, have the API return the updated (or created) representation as part of the response. In case of a POST that resulted in a creation, use a HTTP 201 status code and include a Location header that points to the URL of the new resource.

204 No Content, In case of API response, it can be simple XML or JSON or any other media type. So the REST API must always return an appropriate status code to the client so that the client 204 No Content: This informs the client that the request has been successfully GET student record, myApp/students/{rollNo}, GET, Success, 200. Most applications don't go this far. Frankly, I haven't seen a single one. Given that, returning 200 with Content-Length: 0 or 204 No Content is an almost completely irrelevant discussion. The HTTP specification certainly doesn't forbid 200 OK with Content-Length: 0. That was a bit of a tangent.

Understanding the REST API Response, From the service's perspective, a 204 (No Content) response may be a perfectly valid A good REST API should make it easy to be a client. this response SHOULD include an indication of the request's current status and [. Returning 204 (No Content) as a response to a GET request is a bit weird, but  POST Method. The RESTful HTTP Request POST method is equivalent to Create functions and INSERT SQL statement. The following example is to insert a new partner, Partner1, in ConfigStore database.

REST lesson learned: Avoid 204 responses, REST APIs are designed to work on top of HTTP/HTTPS protocol. 204 NO CONTENT — It indicates that the HTTP request was successful but no content is Using Proper HTTP Status Codes In A REST API. GET Request. Many times, the action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either HTTP response code 200 (OK) or 204 (No Content) is the appropriate response status. Responses to this method are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields.

Comments
  • You probably should return 404 since entity cannot be found.
  • I don't want the client to think something wrong happened. In my case, it's perfectly ok if a primary customer doesn't exist.
  • 404 does not mean that something wrong has happened. It just indicates that the requested resource does not exist. Otherwise how does client know that it got no resource vs resource exists but it has no representation.
  • This seems to be a matter of opinion. See stackoverflow.com/q/34312023/217324, there are arguments for both sides.
  • @NathanHughes in that question the different situation is discussed namely when the list of entities is accessed and it is empty.
  • Cacheability of a response can be set explicitly with a Cache-control header, so you can set Cache-control: max-age=0 on your 204 response.
  • You are right. Caching is also valid for HTTP 200. Removed this line.