Hypertext Transfer Protocol (HTTP) is a fundamental protocol that enables the exchange of information over the internet. It allows web pages and other data to be transferred from servers (where websites are hosted) to clients (typically web browsers) across a network. Invented by Tim Berners-Lee in the early 1990s, it is one of the cornerstones of the World Wide Web.

HTTP uses the client-server model. In this model, a client (such as a user’s browser) sends a request to a server (such as the server hosting a website), and the server responds to this request. This request-response mechanism allows the transfer of web pages, images, videos, and other content across the internet.

HTTP Requests

HTTP requests are composed of four main components:

  • URL
  • Method
  • Headers
  • Request Body (optional)

Below is an example of an HTTP header:

GET /index.html HTTP/1.1
Host: www.example.com
Cookie: SESSIONID=badb655ebd99ed6c8e58c0a1aab44eb9
  • The first line contains the HTTP Method, the path of the request, and the HTTP protocol version. In our example, the request is sent using the

    GET

    method to the

    index.html

    path. The HTTP version is specified as

    HTTP/1.1

    .

  • The second line shows the address of the destination server. The target server’s address can be represented by a domain name or an IP address. In our example, the request is sent to the

    www.example.com

    domain.

  • The third line includes a cookie header, which is used for client-side data storage. Login processes on websites are often managed using cookies. In this example, a cookie named

    SESSIONID

    with the value

    badb655ebd99ed6c8e58c0a1aab44eb9

    is sent to the target server to identify the client.

HTTP Request Methods

HTTP request methods tell the server what kind of action the client wants the server to perform. The most common methods are:

HTTP MethodDescription
GETUsed to retrieve a resource; returns the content of the resource specified by the URL.
POSTUsed to send data to the server; typically used for submitting form data.
PUTUsed to create or update a specific resource.
DELETEUsed to delete a specific resource.
HEADSimilar to GET, but returns only headers instead of the resource itself.
OPTIONSUsed to query the available HTTP methods supported by a resource.
PATCHUsed to partially update a specific resource.
CONNECTUsed to establish a tunnel between the client and server.
TRACEUsed to perform a message loop-back test along the request path.

HTTP Request Headers

HTTP request headers contain additional information sent to the server as part of an HTTP request. These headers provide context and additional instructions on how the request should be processed, the client’s preferences, the type of content being sent, and more. They make the communication between the client and server more effective and meaningful.

Headers are written in a name/value format, as shown in the example below:

NAME=VALUE

These headers provide the server with context and additional instructions that can be used to process the request or customize the response.

An example of an HTTP header:

GET /api/info HTTP/1.1
Host: example.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.82 Safari/537.36
Accept: application/json
Accept-Language: en-US,en;q=0.5
Authorization: Token secret123
Cache-Control: no-cache
Connection: keep-alive
Referer: https://www.google.com/
Pragma: no-cache

In this example, a request is sent to the

/api/info

path using the

GET

method with the

HTTP/1.1

protocol. The request includes the following 10 header examples:

Header NameDescription
HostSpecifies the hostname of the server the client is trying to connect to.
User-AgentProvides information about the client making the request (the User-Agent value indicates it is a Chrome browser).
AcceptSpecifies the MIME types the client is willing to accept in the response.
Accept-LanguageSpecifies the preferred language(s) for the response.
AuthorizationProvides an access token for authentication purposes.
Cache-ControlSpecifies caching directives for both the request and response.
ConnectionSpecifies options for managing the connection between the client and server.
RefererSpecifies the URL of the referring page to the current request page.
PragmaProvides implementation-specific directives that may apply to any agent along the request-response chain.
Content-TypeSpecifies the MIME type of the data sent in the request body, but it is not used in this example as this is a GET request without a request body.

HTTP Request Body

The HTTP request body contains the data packaged to be sent from the client to the server. It is typically included in POST, PUT, PATCH, and similar methods. The data can be form data, files, or structured data like JSON or XML. For example, when a user fills out and submits a form, the form data is sent to the server in the request body; or when JSON data needs to be sent to an API, that data is included in the request body.

The content and format of the HTTP request body are determined by the

Content-Type

header. This helps the server understand how to process the data being sent. For instance, the

Content-Type: application/json

header indicates that the body contains JSON formatted data, and the server should process it accordingly. This mechanism allows for more complex and meaningful data exchanges between client and server, making web applications and APIs dynamic and interactive.

An example of an HTTP header with a Request Body:

POST /v1/photos/upload HTTP/1.1
Host: api.myphotos.com
x-api-key: API_KEY
Content-Length: 232
Content-Type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW
 
------WebKitFormBoundary7MA4YWxkTrZu0gW
Content-Disposition: form-data; name="file"; filename="/C:/Users/John/Photos/my_photo.jpg"
Content-Type: image/jpeg
 
(data)
------WebKitFormBoundary7MA4YWxkTrZu0gW--

In the request above, a photo located at

C:/Users/John/Photos/my_photo.jpg

on the local computer is being uploaded to the API endpoint at

/v1/photos/upload

on the

api.myphotos.com

server using the POST method.

When the server receives this request, it will parse the request body and use it to create a new entry in the photo database.

HTTP Responses

An HTTP response is the reply from a web server to a client’s HTTP request. This response contains the status of the requested resource, information about the server itself, and optionally, the requested resource.

HTTP/1.1 200 OK
Date: Sun, 28 Mar 2023 10:15:00 GMT
Content-Type: application/json
Server: Apache/2.4.39 (Unix) OpenSSL/1.1.1c PHP/7.3.6
Content-Length: 1024
 
{
    "name": "John Doe",
    "email": "johndoe@example.com"
}

The first line, status line, indicates the HTTP version and a status code signaling the result of the request.

Here is a table listing the most common status codes:

Status CodeMeaning
100Continue
101Switching Protocols
200OK
201Created
202Accepted
203Non-Authoritative Information
404Not Found: The requested resource was not found on the server.
500Internal Server Error: The server encountered an error while processing the request.
301Moved Permanently: The requested resource has been permanently moved to a new URL.

From the

Content-Type

header in the example, we can infer that the body of the response is in

application/json

, meaning it is in JSON format. The response indicates that the information for the user named

John Doe

is being returned as a result of the request.

Step-by-Step Operation

To summarize what we’ve learned, let’s look at the steps involved when accessing a website:

  1. Opening the Web Browser and Entering a URL

To start browsing the internet, you first open a web browser and enter the address of the website you want to visit, known as the URL (Uniform Resource Locator). For example: https://google.com

  1. DNS Query

The URL you entered is a human-readable address that is easy to remember. However, internet networks and computers use numerical IP addresses instead of these names. Therefore, your browser performs a Domain Name System (DNS) query to convert the URL into the IP address of the server hosting the website. DNS works like a phone book for the internet; it takes the website name you entered and converts it to the corresponding IP address.

  1. Sending a Request to the Server

With the IP address obtained from the DNS query, your browser sends an HTTP request to the server hosting the website. This request asks the server for the content of a specific page.

  1. Receiving the Response from the Server

The web server receives your browser’s request and processes it, returning the content of the requested web page in an HTTP response. This response typically consists of HTML (Hypertext Markup Language) code, which defines how the page should look.

  1. Displaying the Web Page

Your browser processes the HTML content received from the server and presents it as a visual web page to the user. At this stage, additional resources (images, CSS files, JavaScript files, etc.) may also need to be requested and fetched from the server to ensure the page is displayed correctly.

  1. Terminating the Connection

After the web page is successfully loaded, the connection between the client and the server is automatically terminated unless the

keep-alive

feature is used in HTTP/1.1. If the user clicks on another link or makes a new page request, this process begins again, establishing a new connection.