What is HTTP?
HTTP (Hypertext Transfer Protocol) is a set of rules that define how data is transferred between a client and server on the internet. The client initiates this data exchange by sending an HTTP request, and the server processes this request and sends an HTTP response to the client.
In this article, we’ll go over how HTTP works and discuss the various components of HTTP requests and responses. We’ll then take a look at the key differences between HTTP/1.1 and HTTP/2—and highlight how Postman can help you work with HTTP.
How does HTTP work?
HTTP is an application layer protocol that facilitates data exchange over a TCP connection, and it is a foundational component of the world wide web.
When a user visits a website, the browser sends an HTTP request to the web server that is hosting the website. The server then responds with the requested data, typically in the form of HTML pages, which can include images or videos. HTTP defines the structure of these requests and responses and is used to facilitate the exchange of information between clients and servers.
The following steps are involved in this process:
- Request: When you enter a URL into your web browser or click on a link, the browser creates an HTTP request. This request includes information about the resource you want to retrieve (i.e., a web page, an image, or a video) and is sent to the server hosting that resource.
- Response: The server processes the request and generates an HTTP response. This response includes the requested resource’s data and relevant metadata, such as the status of the request or errors (if there were any).
- Data transfer: The server transmits the response back to the client (i.e., your web browser) over the internet using the HTTP protocol.
- Rendering: The web browser interprets the received data and renders it on your screen, displaying the web page or content as intended.
Under the hood, the client is able to send an HTTP request by opening up a TCP connection to the server. Once the connection is established, the client is able to send an HTTP message via the open connection. The message includes the request method, the request headers, the host, and any other relevant information. When the server is done processing the request, it sends an HTTP response over the TCP connection with the requested data and appropriate metadata.
What is included in an HTTP request?
An HTTP request is a message sent by the client to the server. HTTP requests are used to retrieve, create, update, or delete data, and they include the following key components that tell the server how to proceed:
- HTTP method
- URL (Uniform Resource Locator)
- HTTP version
- Headers
- Body (optional)
HTTP method
The HTTP method represents the action the client expects the server to perform on the resource. The common HTTP request methods are:
- GET: Indicates that the client is trying to fetch a specific resource from the server.
- POST: Indicates that the client is trying to add new data, which needs to be processed by the server.
- PUT: Indicates that the client would like to update an existing resource on the server.
- DELETE: Indicates that the client is trying to delete a resource from the server.
URL
The URL points to the specific resource the client wants to access. An HTTP URL includes a scheme (HTTP), a domain name, an optional path parameter, optional query parameters, and optional anchors. Let’s take the following URL, for example:
https://learning.postman.com/docs/introduction/overview/#getting-started
This URL can be broken down into the following components:
- Scheme: https
- Domain: learning.postman.com
- Path: /docs/introduction/overview
- Anchor: #getting-started
HTTP version
The HTTP version indicates the version of the HTTP protocol being used, such as HTTP/1.1 or HTTP/2. The main difference between HTTP/1.1 and HTTP/2 is that HTTP/2 uses multiplexing and header compression to significantly improve the efficiency and speed of web page loading compared to the older, more linear request/response model of HTTP/1.1. Multiplexing allows browsers to send multiple requests on the same HTTP connection and receive responses in that exact order.
Headers
HTTP headers are key-value pairs that provide additional information about the request, such as the user agent (information about the client’s browser or application), the type of data the client can accept, and cookies for maintaining user sessions.
The example request below includes several HTTP headers:
GET /index.html HTTP/1.1 Host: www.example.com User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/95.0.4638.54 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8 Accept-Language: en-US,en;q=0.9 Connection: keep-alive
This GET request, which indicates that the client is requesting the index.html resource and is using HTTP/1.1, includes the following headers:
- Host: Specifies the domain name of the server to which the request is sent.
- User-Agent: Provides information about the client’s web browser or application.
- Accept: Indicates the types of content the client can accept.
- Accept-Language: Specifies the client’s preferred languages for content.
- Connection: Specifies how the connection between the client and server should be managed.
Body
In HTTP requests like POST and PUT, where a new record is being created or modified on the server, a request body can carry data to be sent to the server, often in formats like JSON, XML, or form data. The below is an example of an HTTP request with a JSON request body:
POST /api/resource HTTP/1.1 Host: example.com Content-Type: application/json Content-Length: 35 { "key1": "value1", "key2": "value2" }
What is included in an HTTP response?
An HTTP response is a message sent by the server after it is done processing an HTTP request. It contains the requested information, along with some related metadata. An HTTP response typically includes the following components:
- HTTP status code: This is a three-digit numeric code that indicates the result of the request, such as 200 OK (successful), 404 Not Found (resource not found), or 500 Internal Server Error (server-side error).
- Response headers: These are key-value pairs that provide additional information about the response, such as its content type, content length, server information, and cookies.
- Body: The response body is the actual data or content that is being returned in response to the request. For example, in the case of a web page, the HTML content of the page would be in the response body. The body can also contain images, videos, serialized JSON data, or any other data requested by the client.
What are the different API architectural patterns that use HTTP?
There are multiple API architectural patterns that are built and modeled around HTTP. They use HTTP as their primary transport protocol and provide a set of rules and guidelines for how HTTP requests and responses should be structured. Some of these API architectures include:
- REST (Representational State Transfer): REST is one of the most widely used API architectural patterns. REST APIs rely on a set of constraints and principles that use standard HTTP methods (GET, POST, PUT, DELETE) to perform only CRUD (Create, Read, Update, Delete) operations on resources in a stateless manner.
- GraphQL: GraphQL is an API architectural pattern that lets the server define a data schema for its data. The client can use this schema to request the exact data it needs and nothing more. It provides a single endpoint for querying, updating, creating, and deleting data. This reduces overfetching and removes the complexity of having to manage multiple API endpoints.
- SOAP (Simple Object Access Protocol): SOAP is a protocol-based API architectural pattern. It uses XML as a message format and can be transported over HTTP. It provides a strict specification for message structure and is typically associated with web services.
What are the differences between HTTP/1.1 and HTTP/2?
HTTP/1.1 and HTTP/2 are both versions of the HTTP protocol that are used for transmitting data over the internet, but there are significant differences between them. HTTP/2 offers several significant improvements over HTTP/1.1, particularly in terms of speed, efficiency, and reduced latency. The following features differentiate them from each other:
Multiplexing
- HTTP/1.1: In HTTP/1.1, each HTTP request/response connection is handled sequentially, which means that a new connection is established for each resource that’s requested. This can lead to a phenomenon known as “head-of-line blocking,” where one slow request can delay the transmission of other resources.
- HTTP/2: HTTP/2 introduces multiplexing, allowing multiple requests and responses to be sent and received in parallel over a single connection. This significantly improves efficiency and reduces latency in loading web pages.
Header compression
- HTTP/1.1: Each HTTP request and response includes a set of headers that convey metadata about the content. In HTTP/1.1, these headers are sent in plain text and can be verbose, leading to unnecessary overhead.
- HTTP/2: HTTP/2 uses header compression, which reduces the size of headers and minimizes overhead. This is achieved through a technique called HPACK, making the protocol more efficient, especially for requests and responses with many headers.
Prioritization
- HTTP/1.1: In HTTP/1.1, there is no built-in mechanism for prioritizing requests. All requests are treated equally.
- HTTP/2: HTTP/2 introduces stream prioritization, allowing clients to specify the importance of different resources. This enables more critical resources to be loaded before less critical ones, enhancing the user experience.
Server push
- HTTP/1.1: Server push is not a feature of HTTP/1.1.
- HTTP/2: HTTP/2 supports server push, where the server can push resources to the client before the client requests them. This improves bi-directional communication between the client and the server.
Connection management
- HTTP/1.1: In HTTP/1.1, multiple connections are often needed to load different resources from a single web page, which can lead to increased latency. For example, when loading a webpage, the client will need to establish a new connection to load the HTML, CSS, JavaScript, and images.
- HTTP/2: HTTP/2 is designed to be more efficient in managing connections. Multiple requests and responses can be multiplexed over a single connection, reducing the overhead associated with establishing multiple connections.
How does Postman help you work with HTTP?
Postman is a multi-protocol API platform that lets you work with several API architectures that use HTTP, including REST, GraphQL, and SOAP. With Postman, you can:
- Work with multiple HTTP API architectures: Postman makes it easy to work collaboratively with different types of HTTP APIs, including REST, GraphQL, and SOAP APIs. For instance, you can group and organize HTTP requests into Postman Collections, save responses, and author and share API documentation. This makes it easier for team members or external developers to understand and use your APIs.
- Capture HTTP traffic and sync cookies: Postman Interceptor enables users to capture and inspect HTTP requests that pass between client applications and their API—and save them to a collection. Users can then leverage the saved request information to understand how certain APIs are behaving and to assist with debugging.
- Easily write tests to validate HTTP requests and responses: Postman offers a suite of tools that enable developers to manually and automatically test REST and GraphQL APIs. For instance, users can leverage a library of pre-built code snippets to quickly author tests for HTTP requests—and chain these requests together to validate complex, end-to-end workflows.
- Create mock APIs: Postman users can leverage saved HTTP responses to create mock APIs from their collections. They can also include dynamic data in these mocked requests, which helps teams collaborate more efficiently.
Great explanation, but does Postman already support that efficient HTTP/2?
or is not yet ready since 2015 the protocol got updated?
Thanks