HTTP Request and Response Message Format

 

HTTP Request and Response Message Format

Introduction

HTTP is a request–response protocol used for communication between a web browser (client) and a web server.

  • The client sends an HTTP request message to the server.
  • The server processes the request and sends an HTTP response message.

Both request and response messages have a well-defined format.


HTTP Message Structure

Both HTTP request and response messages consist of four sections:

Request Message    Response Message
Request Line    Status Line
Header Lines    Header Lines
Blank Line    Blank Line
Body (Optional)    Body (Optional)
    



HTTP Request Message

An HTTP request message is sent from the client to the server to request a web page or perform another operation.

Format

Request Line
Header Lines
Blank Line
Body (Optional)

1. Request Line

The request line consists of three fields:

Method   URL   HTTP Version

Example

GET /index.html HTTP/1.1

Fields

FieldDescription
Method        Specifies the type of request
URL        Identifies the requested resource
Version        Specifies the HTTP version

HTTP Methods

MethodPurpose
GET    Retrieve a document from the server
HEAD    Retrieve only header information
PUT    Upload a document to the server
POST    Send data to the server
TRACE    Echo the received request (debugging)
DELETE    Delete a web page from the server
CONNECT    Reserved (used mainly by proxy servers)
OPTIONS    Request available communication options

GET is the most commonly used method for retrieving web pages.



2. Header Lines

Header lines carry additional information from the client to the server.

Each header has the format:

Header-Name: Value

Common Request Headers

HeaderPurpose
User-Agent    Identifies the browser/client
Accept    Media types accepted by the client
Accept-Charset    Supported character sets
Accept-Encoding    Supported encoding schemes
Accept-Language    Preferred language
Authorization    Authentication information
Host    Server name and port number
Date    Current date
Upgrade    Preferred communication protocol
Cookie    Returns stored cookies
If-Modified-Since    Requests file only if modified

3. Blank Line

A blank line separates the header section from the body.


4. Body (Optional)

The body contains data sent by the client.

It is mainly used with:

  • PUT
  • POST

Examples:

  • Form data
  • Uploaded files
  • Comments

For a GET request, the body is usually empty.


HTTP Response Message

An HTTP response message is sent by the server after processing the client's request.

Format

Status Line
Header Lines
Blank Line
Body (Optional)

1. Status Line

The status line consists of three fields:

HTTP Version   Status Code   Status Phrase

Example

HTTP/1.1 200 OK

Fields

FieldDescription
Version    HTTP version used
Status Code        Indicates the result of the request
Status Phrase    Text description of the status code

HTTP Status Codes

Code RangeMeaning
100–199    Informational messages
200–299    Successful request
300–399    Redirection
400–499    Client-side errors
500–599    Server-side errors

Examples

CodeMeaning
200 OK    Request successful
301 Moved Permanently    Resource moved
404 Not Found    Requested page not found
500 Internal Server Error    Server error

2. Header Lines

Response headers provide additional information about the server and the returned document.

Each header has the format:

Header-Name: Value

Common Response Headers

HeaderPurpose
Date        Current date and time
Server        Server software information
Content-Type        Type of document returned
Content-Length        Size of the document
Content-Language        Language of the document
Content-Encoding        Encoding scheme used
Last-Modified        Date of last modification
Location        Redirects the client to another URL
Set-Cookie        Sends cookies to the client
Accept-Ranges        Indicates supported byte ranges
Upgrade        Preferred communication protocol

3. Blank Line

Separates the header section from the body.


4. Body (Optional)

The body contains the actual resource requested by the client.

Examples:

  • HTML page
  • Image
  • PDF document
  • Audio or video file

The body is absent when:

  • The response is only header information (e.g., HEAD request)
  • An error response contains no resource

Example

This example retrieves a document . We use the GET method to retrieve an image with the path /usr/bin/image1. The request line shows the method (GET), the URL, and the HTTP version (1.1). The header has two lines that show that the client can accept images in the GIF or JPEG format. The request does not have a body. The response message contains the status line and four lines of header. The header lines define the date, server, content encoding (MIME version, which will be described in electronic mail), and length of the document. The body of the document follows the header


Example 

In this example, the client wants to send a web page to be posted on the server. We use the PUT method. The request line shows the method (PUT), URL, and HTTP version (1.1). There are four lines of headers. The request body contains the web page to be posted. The response message contains the status line and four lines of headers. The created document, which is a CGI document, is included as the body.

Example
Conditional Request

A client can add a condition in its request. In this case, the server will send the requested web page if the condition is met or inform the client otherwise. One of the most common conditions imposed by the client is the time and date the web page is modified. The client can send the header line If-Modified-Since with the request to tell the server that it needs the page only if it is modified after a certain point in time.
The following shows how a client imposes the modification data and time condition on a request.



GET http://www.commonServer.com/information/file1 HTTP/1.1 Request line
If-Modified-Since: Thu, Sept 04 00:00:00 GMT                             Header line
                                                                                                          Blank line
The status line in the response shows the file was not modified after the defined point in time.
The body of the response message is also empty.

HTTP/1.1 304 Not Modified Status line
Date: Sat, Sept 06 08 16:22:46 GMT First header line
Server: commonServer.com Second header line
Blank line
(Empty Body) Empty body


Comparison of Request and Response Messages

FeatureRequest MessageResponse Message
Sent By        Client        Server
First Line        Request Line        Status Line
Contains        Method, URL, Version        Version, Status Code, Status Phrase
Header Lines        Client information        Server and document information
Body        Optional (mainly POST/PUT)        Requested document or error information

Summary

In HTTP communication, the client sends a request message to ask for a resource, and the server returns a response message containing the requested resource or an error message. Both messages consist of four sections: a request/status line, header lines, a blank line, and an optional body. The request line specifies the HTTP method, URL, and version, while the status line contains the HTTP version, status code, and status phrase indicating the result of the request.

Comments

Popular posts from this blog

Computer Networks PCCST501 Semester 5 KTU CS 2024 Scheme - Dr Binu V P

Introduction to Computer Networks

Introduction to Local Area Network (LAN)