What status code to use when there is no database record to return on an HTTP request?

Asked

Viewed 2,792 times

7

I own an endpoint that returns a list of products.

Therefore, if there is data from this list I set the HTTP status code to 200, and if there is not, I return only a json saying that no record has been found and the HTTP 200 status code.

My endpoint:

http://api.minhaappexemplo.com.br/rest/lista/1/minha111chave222

My routine responsible for this endpoint, see:

<?php  
$app->get('/rest/lista/{id}/{key}', 
    function(\Psr\Http\Message\ServerRequestInterface $request, \Psr\Http\Message\ResponseInterface $response, $next)
    {
        $id = $request->getAttribute("id");

        $listaProdutos = Lista::obterListaProdutos($id);

        if ($listaProdutos)
        {            
            return $response->withJson($listaProdutos, 200);
        }
        else 
        {            
            return $response->withJson(array("resposta" => 'Nenhum registro encontrado.'), 200);
        }
    }
)->add(new MiddlewareApiKeyCheck());

My questions are related to HTTP status code.

Doubts

  1. Is there any HTTP status code that can be used in situations that no records to return to client/browser?
  2. Can I use status code 200 in these situations? Since the successful request status must be 200 OK.

2 answers

5


Use the 404

Behold in this blog. He explains very well.

Summary of blog comments on the return 204:

  1. 204 No Content Not extremely useful as a response code for a Browser (although, according to HTTP specification browsers, you need to understand it as a response code "do not change the view").
  2. However, the 204 No Content is very useful for ajax web services that You might want to indicate success without having to return something. (Especially in cases like DELETE or Posts that do not require comments).

The answer, therefore, to your question is to use 404 in your case. 204 is a specialized response code that you normally should not return to a browser in response to a GET.

Other response codes are even less appropriate than 204 and 404.

  1. 200 shall be returned with the body of the one you are successfully sought . It is not appropriate when the entity you are looking for does not exist.
  2. 202 is used when the server started working on an object, but the The object is not quite ready yet. Certainly, not the case here.
  3. 400 is used in response to a poorly formatted HTTP request (for Instance of malformed HTTP headers, incorrectly ordered segments, etc.). This will almost certainly be handled by whatever structure you are using. You should not deal with this unless you are writing Your own server from scratch. Newer Rfcs now allow 400 to Be used for semantically invalid requests.

Definitions of Wikipedia. You can also see the settings in www.W3.org

Source of the answer

1

I use 204 No content, I’ve found that some libraries also use this same code in similar situations

Browser other questions tagged

You are not signed in. Login or sign up in order to post.