Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 327
Elisabeth Avatar asked Dec 01 '25 05:12

Elisabeth


2 Answers

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 :)

like image 127
Spencer Avatar answered Dec 03 '25 18:12

Spencer


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.

like image 33
Maksood Avatar answered Dec 03 '25 17:12

Maksood