Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does every successful HTTP request always return status code 200?

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?

like image 720
Jerry Dodge Avatar asked Feb 28 '13 20:02

Jerry Dodge


People also ask

Can I return a 200 OK HTTP status?

Yes, You can return 200 Ok HTTP status, but you SHOULD return a response BODY. In general, we have 3 options according to your API requirements: Return 201 Created HTTP status, with EMPTY BODY. In case you don't need to return a response body.

What are the HTTP response status codes?

HTTP response status codes indicate whether a specific HTTP request has been successfully completed. Responses are grouped in five classes: Informational responses (100–199) Successful responses (200–299) Redirects (300–399) Client errors (400–499) Server errors (500–599)

Why does my http11processor return a response with status 200?

When a Http11Processor that contains an already committed Response object, the Response can not be used to return any information. It simply responds with Status: 200, Content-Length: 0. This seems to be happening when I close a website that has an Atmosphere connection that uses Server Side Events.

What does HTTP status code 200 mean?

If you're trying to figure out what a certain status code means and how to fix it, you're in the right place. In this tutorial, we'll do an overview of HTTP status code 200. An HTTP status code 200 means success. The client has requested documents from the server. The server has replied to the client and given the client the documents. All is well.


2 Answers

"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).

like image 131
kobik Avatar answered Oct 17 '22 16:10

kobik


To answer your specific question:

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

The answer is Yes, because TIdHTTPWebBrokerBridge wraps TIdHTTPServer, which always sets the default response code to 200 for every request, unless you overwrite it with a different value yourself, or have your server do something that implicitly replies with a different response code (like Redirect() which uses 302, or SmartServeFile() which uses 304), or encounter an error that causes TIdHTTPServer to assign a 4xx or 5xx error response code.

However, in general, what others have told you is true. On the client side, you should handle any possible HTTP success response code, not just 200 by itself. Don't make any assumptions about the server implementation.

In fact, TIdHTTP already handles that for you. If TIdHTTP encounters a response code that it considers to be an error code, it will raise an EIdHTTPProtocolException exception into your code. So if you don't get an exception, assume the response is successful. You don't need to check the response code manually.

If there is a particular response code that normally raises an exception but you do not want it to, you can specify that value in the optional AIgnoreReplies parameter of TIdHTTP.Get() or TIdHTTP.DoRequest(). Or, if you are are using an up-to-date Indy 10 SVN revision, a new hoNoProtocolErrorException flag was recently added to the TIdHTTP.HTTPOptions property so the EIdHTTPProtocolException exception is not raised for any response code.

like image 9
Remy Lebeau Avatar answered Oct 17 '22 16:10

Remy Lebeau