How does HTTP work?

Share

Overview

HTTP (Hypertext Transfer Protocol) is a method that allows the transfer of hypertextHypertext refers to a word, phrase or body of text that can be linked to another document or text. while adhering to a particular protocolA protocol is a standardized set of rules for formatting and processing data.. In the context of the internet, HTTP enables web pages to be linked together.

widget

Following the client/server model, HTTP functions on the application layer, allow the data to be sent/received on TCP connections established between the client and server.

Note: To learn more about HTTP, refer to this Answer

How does HTTP work?

For starters, HTTP works entirely through HTTP messages. There are two types of HTTP messages:

  • Request: These are messages sent by the client to the server to trigger an action
  • Response: These are messages sent by the server to the client in response to the request message

We can see that exchanging these messages in a request/response cycle enables data exchange between a client and server.

Figure 1: Request/response cycle

Figure 2 below illustrates the outline of a typical HTTP message. It's divided into three fields: start line, headers, and body. However, keep in mind that the request and response messages have a few key differences in the content that go into each of these fields that will be discussed in their respective sections.

Figure 2: A typical HTTP message

HTTP request messages

Figure 3 depicts the structure of a typical HTTP request message and the data it contains in its fields.

Figure 3: An HTTP request message

Start line

As shown in Figure 3, the start line for the request messages contains the following:

HTTP method

The HTTP method is a command that tells the server what type of request the client is making.

There are multiple methods, namely the following:

  • GET
  • POST
  • PUT
  • PATCH
  • DELETE

Note: To learn about these methods in detail, refer to this Answer.

Request target

The request target, which is most often a URL, contains the location of the resource corresponding to the request message. For example, the request-target in a GET message would include the URL for a file the client wants from the server.

HTTP response messages don't contain a request-target as they already contain the client's requested resource.

HTTP version

This is the version of HTTP that the message is structured with. The following are HTTP versions as formatted for an HTTP message:

  • HTTP/0.9
  • HTTP/1.0
  • HTTP/1.1
  • HTTP/2.0

Headers

The headers for both request and response messages are in the form of one or more name:value pairs. These pairs can be divided into several groups:

  • General
  • Request
  • Representation

Body

The body for the request messages is often empty since most HTTP methods (such as GET and DELETE) are requesting a resource from the server. Request methods such as PUT include the resource to be stored on the server.

HTTP response messages

Figure 3 depicts the structure of a typical HTTP response message and the data it contains in its fields.

Figure 4: An HTTP response message

Start line

As shown in Figure 4, the start line for the response messages contains the following:

HTTP version

It has the same purpose as described for HTTP request messages.

Status code

The status code indicates the success/failure of the request sent in the HTTP request message. Multiple status codes can be divided into the following groups, as illustrated in Figure 5.

Figure 5: Status codes

Headers

These headers follow the same structure as those in HTTP request messages but differ in their types. Response messages can be divided into the following groups:

  • General
  • Request
  • Representation

Body

The response message body contains the resource requested in the request message. For example, a response message sent to a GET request message will have the requested resource in the message's body.

Persistent vs non-persistent HTTP

The communication in persistent and non-persistent HTTP follows the exact mechanism as aforementioned: the message types, structures, and request/response cycle are identical. However, there's one critical difference.

In non-persistent HTTP, the client opens a TCP connection to the server, sends at most one object over the connection using the request/response, and then closes the connection. Thereby, downloading multiple objects requires multiple connections.

In persistent HTTP, the client opens a TCP connection to the server. Multiple objects can then be transferred over this connection if needed through individual request/response cycles, and then the connection closes the connection.

Note: To learn more about persistent vs non-persistent HTTP, refer to the following Answer.

Copyright ©2024 Educative, Inc. All rights reserved