简体   繁体   中英

What to return when a route/url is found but not the resource behind it?

When a route customer/1 exists but the resource/entity behind the customer search does not exist,

Should I return a 404? I mean the route exists...

or should I return a

204 (No content) because I could not find a customer and the result is empty.

Microsoft sample:

public IHttpActionResult Get (int id)
{
    Product product = _repository.Get (id);
    if (product == null)
    {
        return NotFound(); // Returns a NotFoundResult
    }
    return Ok(product);  // Returns an OkNegotiatedContentResult
}

In my opinion a NotFound() => 404 is not correct here?!

CLARIFICATION/IMPROVEMENT of my question.

I need to distinguish between a

1) route: customer/1 => route does not exist

( I would return here 404, but this is anyway handled by the framework...)

2) route: customer/1 => exists but the resource/entity in database NOT

( I would return here a 404)

3) route: customersSearch/searchterm => the resource/entity in database is not found

( I would return a 204 )

Would you please be that kind and correct your answer to this more clear question please?

General REST guidelines state that in this instance you should return a 404. You have asked for a specific resource that doesn't exist. As if you had requested a physical document from a website.

At all my work places that has been the pattern and issued by the enterprise architecture teams :)

A word of advice though... have some form of logging for this scenario so you can detect a "logical" 404 (in this scenario) versus a true no resource / handler / script mapping was available to service the request! At least to identify issues at deploy time :)

Its a design question but I agree with you that 404 is probably not the appropriate status code here since 4xx means client error. I would pick something from 2xx(204 is indeed a good choice here) or 5xx. For your reference, here's RFC for http status codes: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html

Based on your update, 204 is an acceptable return code.

I tend to use a custom HTTP Reason Phrase to differentiate that the 404 was due to a resource not being found as opposed to a malformed URL resulting in an incorrect Route. The 404 is still the correct response code to use under all 3 scenarios, yet if you are trying to make development time easier by differentiating the reasons, then use a different reason phrase.

eg

  1. Incorrect route returns default 404 Not Found
  2. Correct route, but no resource with that ID found 404 Customer Not Found
  3. Correct route, but no resources found that match the search criteria 404 No Customers Found

I interpret 204 as different from the above comments in that the 204 spec states (emphasis mine):

The server has fulfilled the request but does not need to return an entity-body

in this scenario, the api did need to return an entity-body, it simply wasn't able to because the requested information wasn't available. Hence it was Not Found

Example of how to return a custom reason phrase in Web API.

public IHttpActionResult Get (int id)
{
    Product product = _repository.Get (id);
    if (product == null)
    {
        var response = Request.CreateErrorResponse(HttpStatusCode.NotFound, "");
        response.ReasonPhrase = "Product Not Found";
        return response;
    }
    return Ok(product);  // Returns an OkNegotiatedContentResult
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM