What Http code should I return for "Thing not found"?

I am creating a web service that is used in this particular case to request information about a patron.

Say, for the sake of argument, that an Internet search hit:

GET /patrons/619 HTTP/1.1 

If a cartridge is found, I return code 200:

 HTTP/1.1 200 OK 

If you omit or specify an account number that is not a number, I return 400. For example, the following bad queries:

 GET /patrons HTTP/1.1 GET /patrons/ HTTP/1.1 GET /patrons/G619 HTTP/1.1 GET /patrons/kirsten%20guyer HTTP/1.1 

all 400 return error (invalid request), for example:

 HTTP/1.1 400 Invalid patron number 

I want the status code not found protector returned as HTTP status code. For example:

 GET /patrons/1322 HTTP/1.1 HTTP/1.1 404 Not Found 

I thought about using 404 (not found) , which is a valid answer (the requested resource was, indeed, in truth, not found.) But I'm afraid that people are debugging it, think that it means that they write /patrons/ .

Can anyone think of another http status code that I could use?


Update: I am watching

 204 No Content The server successfully processed the request, but is not returning any content. 

What are you saying?


Keep in mind that not all HTTP servers serve HTML content. If a resource is proposed on the IIS web server with the name:

 GET /MyStartPage.html HTTP/1.1 

Then the HTTP server should decide what to respond. On most web servers, a resource named /MyStartPage.html corresponds to a file located on the hard drive.

While StackOverflow does:

 GET /posts/1027301 HTTP/1.1 

What if this resource does not exist, the web server should (correctly) return 404.

+7
web-services lookup
source share
9 answers

404 Not Found is a sure thing to return, if it is a service, it is really not used by people, but by machines and, therefore, typos, should not be your first concern.

In addition, you can do very little to resist human behavior anyway (thinking about one thing when it’s really different). If you return a small msg error as part of the error code, everything should work out. You can even offer them a possible solution.

The return of 500, when the application does exactly what it developed, also seems a little strange. 404 accurately describes the situation: the resource was not found.

+17
source

I think you should use 404. API developers will understand what 404 means, and therefore will follow their own normal debugging process to solve the problem. Stick to the protocol.

+7
source

Error 404 is actually a more acceptable answer for this case, because the resource was really not found. You can always have a special document with an error that explains that it is a Patron identifier that was not found.

Error 400 means that the client sent the wrong syntax, which is not the case in your example. Therefore, you should not use this error code. It may seem that the “bad request” is accurate, but in fact it means that there is an error in the syntax of the request header.

This is also not a 500 error because the error did not occur. There is no problem with the web server executing the request, the fact is that the request is looking for a resource that is not there.

404 is the only suitable answer, if you do not want to consider it a fully valid request, return the status of 200 and a page explaining that the requested patron does not exist.

From my initial comment on the question:

I don’t think a 200-level error would be acceptable, consider the explanations in W3 w3.org/Protocols/rfc2616/rfc2616-sec10.html

+6
source

IMHO, the HTTP response is not suitable for this, because the error is not at the HTTP level. This is at the application level. One possible solution is to use a Null Object Pattern to return a null "Patron" that matches the Patron interface but indicates that no such entity exists.


UPDATE: I was convinced that this answer is incorrect. However, I want to leave it as it is a potentially valid answer (depending on the details not provided in the question), and I think the comments are instructive.

+5
source

Usually, if your service detects an error that cannot be processed, but the request is well-formed, then I think you will want to return a 500 status code.

Also, another reason why I say 500 would be appropriate is because of my experience using .NET web services whenever I throw an exception in explorer (like RecordNotFound exception), web server and client always interpret the response will consist of a status code 500.

In any case, it would be nice to review this list of http status codes, and excellent, the one that suits your needs best.

+2
source

It depends on what type of web service you create.

SOAP and XML web services usually return 200 OK if the requested object (cartridge) cannot be found and encodes the type / error message / description in the response document. They separate the transport layer (HTTP) from messaging (XML). Error 404 (which is at the transport level) is returned only if you request a non-existent API endpoint (invalid URL).

However, when using the REST web service, you use HTTP methods and statuses directly for messaging. REST uses different HTTP methods (GET / POST / PUT / DELETE) to indicate what action is required, and the server returns the status status of HTTP. Thus, in the case of a web service, REST 404 will be the correct HTTP status if no patron could be found.

500 is inappropriate anyway, I think, since 5xx means that something was wrong on the server (it is not), while 4xx errors mean that something was wrong on the client side.

+1
source

It may be 4 years, but it is still pretty relevant. For APIs that should be used by both servers and native applications, I find it much easier to use HTTP codes to indicate status. And if necessary, use a few additional codes from unallocated for conditions that are not suitable for existing HTTP codes.

+1
source

Errors at the web service level should not only return a simple HTTP status bar, but instead should return content in a valid and documented format that can be analyzed by the client accessing your service. The returned document should contain an error code, which is part of a documented set of codes specific to your web service, as well as a brief description of the problem and, possibly, a more detailed description of the problem.

0
source

If you want to control web service responses through HTTP response codes, then you can really use 404 to indicate this condition.

However, I find it more natural for the web service response to be fully defined in the text of the HTTP response and just return 200 for a successful connection ("I understood your request and sent you the appropriate response"), In this case, you return 200 as usual with the payload of your request (XML or JSON or something else) indicating that no matching object was found

I would consider non-200 error codes similar to exceptions in common programming languages, and the HTTP response body is more like a return code. Imagine if you are doing this conditionally - would you throw an exception if no entity was found, or return null ? Personally, given the fragility of network connections, I would like to reserve all the error codes at the HTTP level for communication problems, and I would rather give 200 OK from the service itself along with a payload indicating the result or any service level error.

0
source

All Articles