2

I am new to .Net 6 Web API, and I am trying to learn. I build a dummy app that you can add and retrieve city. Currently, when I send an id to find a city. If the idea does not exist I return the NotFound(), and the response looks like

{
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.4",
    "title": "Not Found",
    "status": 404,
    "traceId": "00-48573a8a76307babb35bad289828ef16-0bdc7cccfcfee10c-00"
}

But when I sent extra information to the NotFound($"City with Id={id} is not found") I get the response like a string.

Is there a way to use NotFound(pass data) and get a response like similar to original response plus a property "Error": "city with id = 3 is not found"

{
    "Error": "city with id = 3 is not found"
    "type": "https://tools.ietf.org/html/rfc7231#section-6.5.4",
    "title": "Not Found",
    "status": 404,
    "traceId": "00-48573a8a76307babb35bad289828ef16-0bdc7cccfcfee10c-00"
}

Thank you for your help.

Serge
  • 40,935
  • 4
  • 18
  • 45
redar ismail
  • 163
  • 2
  • 13
  • 2
    Why would you want to send such an error? A 404 is descriptive just for this reason. – klekmek Aug 24 '22 at 13:24
  • 1
    Check this https://stackoverflow.com/questions/20139621/how-do-i-return-notfound-ihttpactionresult-with-an-error-message-or-exception – Dimitris Maragkos Aug 24 '22 at 13:36
  • 1
    "I get the response like a string."Can you show this string pls? – Serge Aug 24 '22 at 13:43
  • Actually when I use if (cityToRemove == null) { return NotFound($"City with id {id} can not be found"); } the response is City with id 7 can not be found – redar ismail Aug 24 '22 at 14:45

1 Answers1

3

It is not the best idea to return 404 NotFound if you can't find some data. It confuses the users since 404 NotFound is usually returned by .NET if the URL is not found.

I highly recommend you to use something else in this case - BadRequest for example. And you can create any JSON you want, if you don't like a string

return BadRequest( new
{
    Error = "city with id = 3 is not found",
    Title = "Record is not found",
    Status = 400
});

It is also a good idea to check the response code, before trying to extract data from API. In this case, the response code could be 400, but certainly not 404.

Serge
  • 40,935
  • 4
  • 18
  • 45
  • Thank you for your help. So it is a bad practice to return { "Error": "city with id = 3 is not found" "type": "https://tools.ietf.org/html/rfc7231#section-6.5.4", "title": "Not Found", "status": 404, "traceId": "00-48573a8a76307babb35bad289828ef16-0bdc7cccfcfee10c-00" } such a response it. it is a good practice to return the "City with id 7 can not be found" – redar ismail Aug 24 '22 at 14:47
  • 1
    @redarismail Yes, now you understand me. 404 should mean a wrong url and nothing else. I didn't tell to return only string. It is a good practice to check a response code. And in this case it should be 400. – Serge Aug 24 '22 at 14:50
  • 4
    statements taken from https://developer.mozilla.org/en-US/docs/Web/HTTP/Status#client_error_responses , "404 Not Found The server can not find the requested resource. In the browser, this means the URL is not recognized. In an API, this can also mean that the endpoint is valid but the resource itself does not exist. Servers may also send this response instead of 403 Forbidden to hide the existence of a resource from an unauthorized client. This response code is probably the most well known due to its frequent occurrence on the web.", so i guess it can be used to tell if id is not found. – CodingMytra Aug 24 '22 at 18:03
  • 1
    and @redarismail 's point was mainly to get the same format in both case with or without specifying Id. – CodingMytra Aug 24 '22 at 18:05
  • 1
    @CodingMytra I updated my answer. I didn't have time for the details and just thought that redar ismail could figure out how to create any kind of message himself. – Serge Aug 24 '22 at 20:04
  • 1
    "It is a bad idea to return 404 NotFound if you can't find some data" - @Serge, who told you this? It is common practice to use protocol messages for modern Rest API. Bad Request means client side error. What error client made to return BadRequest to him? (Data just did not found, no any client side errors here) – Ryan Aug 24 '22 at 21:58
  • 1
    @Ryan Just for the future if you deside to became a software developer. The server usually generates 500 error if there an exception that is not handle. It depends on the application architector. In some cases it is possible just return ok with the error message. My point is that if a developer wants to return an Http error code, in this case 400 is much better choise then 404. – Serge Aug 24 '22 at 22:06
  • 1
    @Serge, but what about http code semantics? 404 means not found, why to ignore protocol? Initial design of HTTP was for for HTML only, page does not exist (no such url)? - return 404. With REST we use other resource (not html) - business entities. Times change, you should adopt to it, as a senior developer.) – Ryan Aug 24 '22 at 22:19
  • 1
    @redarismail, I would suggest to use problemdetails object if you want to get same format and also correct content type in case of bad request. – CodingMytra Aug 25 '22 at 04:14
  • 1
    @Serge a 400 means that the request is incorrect. This can be due to formatting, semantics, syntactics or whatever. A 400 should not be used to say the resource cannot be found because that has nothing to do with the way the request has been made. – klekmek Aug 25 '22 at 07:25
  • 1
    @klekmek But in this case request was incorrect. It contained an incorrect Id , this is why the record was not found in DB. All of you are confused, you have to understand differendce between an incorrect URL that will return that a controller or action is not found and an incorrect record Id . In this case a controller and action will be found and code will run but because of incorrect Id that was send in a request , a record could not be found. – Serge Aug 25 '22 at 09:51
  • 2
    @Serge No, the request was correct. It just contained an id that does not exist. A 400 BadRequest means that the request was malformed, indicating to the client that he set up his request incorrectly and thus has to look into why this is the case. A 404 tells the client he made a correct request but requested an unknown entity. There isn't much debate as this is general consensus. Just take a look at the APIs of larger companies and see what they do. – klekmek Aug 26 '22 at 07:27
  • well. /api/automobile/5 is a URL, correct? It points at the 5th record. "The 404 (Not Found) status code indicates that the origin server did not find a current representation for the target resource or is not willing to disclose that one exists. A 404 status code does not indicate whether this lack of representation is temporary or permanent" (https://www.rfc-editor.org/rfc/rfc9110.html#name-404-not-found) REST. Representational state transfer. 404: the origin server did not find a current representation. 404 is a valid reponse for not finding a valid representation of the data – Robert Achmann Mar 21 '23 at 15:41