What is HTTP?
HTTP (HyperText Transfer Protocol) is a protocol --- a set of rules for data transmission between computers that allows us to fetch resources like HTML pages. It is the foundation of the World Wide Web, which enables users to browse web pages. Data is exchanged between a client (usually a browser) and a server. The client sends a request, and the server responds with the requested data.
What is HTTPS?
HTTPS stands for HyperText Transfer Protocol Secure. It enhances HTTP by encrypting the data being transmitted. This encryption protects sensitive information from being intercepted by unauthorized parties.
How HTTP Works
Client and Server Communication
A client, often referred to as a user agent (like a browser or web app), sends a request for resources such as HTML, CSS, JavaScript, or images. When a link is clicked, the client initiates a request to the server.
Intermediary Layers
Between the client and server, there are intermediary layers, collectively known as proxies. These can handle tasks like caching, filtering, load balancing, authentication, and logging.
Statelessness of HTTP
HTTP is a stateless protocol, meaning once the data exchange is complete, the connection between the client and server is terminated. To maintain a state (e.g. keeping a user logged in), servers use cookies.
HTTP Messages: Request and Response
The diagram below, from MDN, illustrates the structure of HTTP messages. While HTTP has evolved through versions like HTTP/1.1, HTTP/2, and HTTP/3, the fundamental principles remain the same. HTTP/1.1 is often considered more readable, while newer versions require specialized tools to interpret the binary structure.
HTTP Request
An HTTP request consists of three key parts: method, header, and body.
Method: the purpose of the request or the action to be performed
Common methods include:
GET: Retrieve data
POST: Submit data to be processed
PUT: Update existing data
DELETE: Remove data
Header (optional): additional information for the server, such as supported formats, authentication credentials, or cache controls.
Body (optional): used to send data
HTTP response
An HTTP response consists of three key parts: header, body, and status code.
Header: provides more detailed context of the response, such as its age, location, or server details.
Body (optional): contains fetched data or requested resources
Status Code: indicates if the request was completed successfully or not and the reason
1XX - Informational Responses
2XX - Successful Responses (e.g. 200 OK)
3XX - Redirection Messages
4XX - Client Error Responses
5XX - Server Error Responses
Refer to the complete list of methods:
HTTP response status codes (MDN)
Underlying Protocols: TCP/IP
HTTP relies on TCP/IP for data transmission over the internet.
TCP (Transmission Control Protocol) ensures reliable data delivery by breaking data into small packets, transmitting them, and reassembling them at the destination.
IP (Internet Protocol) handles addressing and routing to ensure the data reaches the correct destination.
As TechTarget explains:
TCP/IP specifies how data is exchanged over the internet by providing end-to-end communications that identify how it should be broken into packets, addressed, transmitted, routed and received at the destination.
HTTP Caching
HTTP caching improves its performance by reducing the loading times and server workload. There are two types: private caches (e.g. browser caches) and shared caches (e.g. proxies and CDNs). You can control caches with headers like:
Cache-Control: private
However, HTTP is designed to store as much as possible without even Cache-Control
being provided. This is called heuristic caching. To prevent caching, you can use:
Cache-Control: no-store
HTTP forms the backbone of the internet, so mastering it can help optimize website performance and improve secure connections. I'll explore more using Postman for hands-on practice.
Top comments (0)