HTTP Response and Status Codes

HTTP Response

An HTTP response is made by a server to a client. The aim of the response is to provide the client with the resource it requested, or inform the client that the action it requested has been carried out; or else to inform the client that an error occurred in processing its request. Below is an example of HTTP response.

An HTTP response contains:

1. A status line.
2. HTTP headers or header fields.
3. Message body

Below is the example of HTTP response :
 HTTP/1.1 200 OK
 Date: Sat, 07 Apr 2018 05:22:04 GMT
 Server: Apache/2.2.16 (Debian)
 X-Powered-By: PHP/5.3.3-7+squeeze15
 X-XSS-Protection: 0
 Vary: Accept-Encoding
 Content-Length: 6033
 Connection: close
 Content-Type: text/html

 <!DOCTYPE html>
 <html>
   <head>
     <title>Test website</title>
   <head>
   <body>
     <p>Hello world!!</p>
   </body>
 </html>

1. A status line :

The status line is the first line in the response message. It consists of three items :

a. The HTTP version number, showing the HTTP specification to which the server has tried to make the message comply.

b. A status code, which is a three-digit number indicating the result of the request.

c. A reason phrase, also known as status text, which is human-readable text that summarizes the meaning of the status code.

In the above example, the response line is :
 HTTP/1.1 200 OK
where :
  • The HTTP version is HTTP/1.1
  • The status code is 200
  • The reason phrase is OK


2. HTTP Headers :

The HTTP headers for a server's response contain information that a client can use to find out more about the response, and about the server that sent it. This information can assist the client with displaying the response to a user, with storing (or caching) the response for future use, and with making further requests to the server now or in the future. HTTP headers allow the client and the server to pass additional information with the request or the response. A request header consists of its case-insensitive name followed by a colon ':', then by its value (without line breaks). Leading white space before the value is ignored. At the above example, the HTTP headers are :
 Date: Sat, 07 Apr 2018 05:22:04 GMT
 Server: Apache/2.2.16 (Debian)
 X-Powered-By: PHP/5.3.3-7+squeeze15
 X-XSS-Protection: 0
 Vary: Accept-Encoding
 Content-Length: 6033
 Connection: close
 Content-Type: text/html


3. Message Body :

A message body is the one which carries the actual HTTP response data from the server ( including files, images, etc.). From the above example, the message will be :
 <!DOCTYPE html>
 <html>
   <head>
     <title>Test website</title>
   <head>
   <body>
     <p>Hello world!!</p>
   </body>
</html>
The message body of a response may be referred to for convenience as a response body. For a response to a successful request, the message body contains either the resource requested by the client, or some information about the status of the action requested by the client. For a response to an unsuccessful request, the message body might provide further information about the reasons for the error, or about some action the client needs to take to complete the request successfully.


HTTP Status Codes :

HTTP response includes status code and reason phrase. The status codes are returned by the servers which is based on the results of a particular request. Status code is a 3-digit integer where the first digit of the Status-Code defines the class of response. There are five categories of status codes :

1xx : Informational - An informational response indicates that the request was received and understood. It is issued on a provisional basis while request processing continues. It alerts the client to wait for a final response.

2xx : Success - This class of status codes indicates that the client's request was successfully received, understood, and accepted.

3xx : Redirection - This class of status codes indicates that the client must take additional action to complete the request. Many of these status codes are used in URL redirection.

4xx  : Client Error - This class of status code is intended for situations in which the error seems to have been caused by the client.

5xx : Server Error - This class of status codes indicates that the server failed to fulfill an apparently valid request.

Some of the common HTTP Status Codes are :
  • 200  OK   ( Found )
  • 301  Moved Permanently
  • 302  Location   ( Redirection )
  • 400  Bad Request
  • 401  Unauthorized   ( Client is not authorized to access the resource )
  • 403  Forbidden   ( Client is not allowed to access the resource )
  • 404  Not Found   ( The requested resource not found on the server )
  • 500  Internal Server Error
  • 503  Service Unavailable



Next Topic :

No comments:

Post a Comment