Does every successful HTTP request always return status code 200?

Jerry Dodge picture Jerry Dodge · Feb 28, 2013 · Viewed 8.6k times · Source

In Delphi, I'm using Indy's TIdHTTPWebBrokerBridge coupled with TIdHTTP to send/receive data via HTTP. On the Server, I don't have any fancy handling, I always just respond with a simple content stream. If there's any issues, I only return information about that issue in the response content (such as authentication failed, invalid request, etc.). So, on the client side, can I assume that every successful request I make to this server will always have a response code of 200 (OK)?

I'm wondering because on the client, the requests are wrapped inside functions which return just a boolean for the success of the request.

Inside this function:

IdHTTP.Get(SomeURL, AStream);
Result:= IdHTTP.ResponseCode = 200;

This function handles any and every request which could possibly fetch data. If there were any issues in the request, This function should return False. In my scenario, since I always return some sort of content on the server, would the client always receive a response code of 200 in this function?

I guess the real question is, if I always return some sort of content and handle all exceptions on the server, then will the server always return status code of 200 to each request?

Answer

kobik picture kobik · Feb 28, 2013

"Does every successful HTTP request always return status code 200?"

See w3.org: HTTP/1.1 Status Code Definitions (RFC 2616)

The answer is No. All 2xx are considered successful. That may depend on the HTTP method used.

Should your web-server application always return 200 upon success? That may as well depend on the request method and the signal it intends for the client . e.g.

for PUT method (emphasis is mine):

If an existing resource is modified, either the 200 (OK) or 204 (No Content) response codes SHOULD be sent to indicate successful completion of the request.

for POST method:

The action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either 200 (OK) or 204 (No Content) is the appropriate response status, depending on whether or not the response includes an entity that describes the result.

If a resource has been created on the origin server, the response SHOULD be 201 (Created) and contain an entity which describes the status of the request and refers to the new resource, and a Location header (see section 14.30). Responses to this method are not cacheable, unless the response includes appropriate Cache-Control or Expires header fields. However, the 303 (See Other) response can be used to direct the user agent to retrieve a cacheable resource.

As you can learn from the RCF, every method SHOULD have it's own success status codes, depending on the implementation.

Your other question:

"can I assume that every successful request I make to this server will always have a response code of 200 (OK)?"

You can always expect Status code 200, if your web server always responds with Status 200. Your web server application controls what response it returns to the client.

That said, Status code 200 is the Standard response for successful HTTP requests (The actual response will depend on the request method used), and in the real world of web servers, SHOULD be set as default upon successful request, unless told otherwise (As explained in Remy's answer).