What status code to use when there is no bank record to return in an HTTP request?

5

I have an endpoint that returns a list of products.

So, if there's any data in this list, I'll set the HTTP status code to 200, and if it does not, I just return a json saying that no record was found and the status code of HTTP 200.

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 concerns are related to the HTTP status code.

Questions

  • There is some HTTP status code that can be used in situations that there no records to return to the client / browser?
  • Can I use status code 200 in these situations? Since the request was successful, status must be 200 OK .
  • asked by anonymous 12.06.2017 / 00:40

    2 answers

    3

    Use 404

    See on this blog . It explains very well.

    Summary of blog comments on return 204 :

  • 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 "do not change display" response code).
  • However, 204 No Content is very useful for ajax web services that You may want to indicate success without having to return something. (Especially in cases such as 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 .

    The other response codes are even less appropriate than 204 and 404 .

  • 200 must be returned with the body of which you are successfully searched . Not appropriate when the entity you are looking for does not exist.
  • 202 is used when the server started working on an object, but the The object is not yet fully ready. Certainly not the case here.
  • 400 is used in response to a badly formatted HTTP request (for Instance of malformed HTTP headers, incorrectly sorted 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. Latest RFCs now allow 400 for Be used for semantically invalid requests.
  • Settings from Wikipedia . You can also view the settings at www.w3.org

    Response font

        
    12.06.2017 / 15:32
    1

    I use 204 In content, I already noticed that some libraries also use this same code in similar situations

        
    12.06.2017 / 01:36