404 Not Found. Resource or location?

Asked

Viewed 53 times

0

Sirs,

I have a webapi whose end point is used to return an employee’s information.

Consider the two scenarios below :

  1. In scenario 1 the URL below is invalid, non-existent. Of course the server returns 404 (Not Found)

https://sistema.com.br/api/v1/employees/1

Obs. employees is incorrect.

  1. In the second scenario, the url is valid, the code falls into the Controller Action, connects to the base by looking for the employee with ID = 1. However, this does not exist. Hence I need to explicitly inform the Status Code for Response.

    ;

        try
        {
            var funcionario = await Task.FromResult(new FuncionarioApplication().ObtemFuncionario(id));
    
            if (funcionario!=null)
            {
                ApiResult.StatusCode = 200;
            }
            else
            {
                ApiResult.StatusCode = 204;
            }
    
        }
        catch (Exception e)
        {
            ApiResult.AddError($"Ocorreu uma Exception ao tentar localizar o funcionario. Detalhe : {e.Message} Origem : {e.StackTrace}");
            ApiResult.StatusCode = 400;
        }
        return base.StatusCode(Result.StatusCode, ApiResult.GetResult());
    

Here I am returning code 204 - No Content

  • 400 Invalid or 416 Requested Request for Unsatisfactory Track

  • if the product does not exist is 404, "does not exist", was not an invalid request

  • I supplemented the question with more details.

  • 404 is perfectly acceptable in its else, since it is just that, something not found. 204 is "found" (or processed), but it will not present anything in the answer. An example of this is a post/put action to save something that wouldn’t need a complete "commit", something like saving automatic drafts of messages that are being edited.Something else, 400 Bad request does not make sense, it would be a problem if it were failure in REQUEST and not on the server side, if an Exception occurred is because there is something very wrong, status 500 Internal Server Error would be more appropriate.

  • Show.. vlw brother

1 answer

0

According to the Wikipedia, Error 404 is an HTTP response code that indicates that the client was able to communicate with the server, but either the server could not find what was requested, or was set to not fulfill the request and not reveal the reason, or the page no longer exists. They should not be confused with other errors where a connection to the target server could not be made.

You should return error 404.

Remember the error codes started at 4 (404, 402, 410, ...) are error codes caused by the HTTP client.

Browser other questions tagged

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