HTTP
Understand the client-server model, remote procedure calls, the HTTP request-response lifecycle, message anatomy, verbs and idempotency, status codes, and TLS/HTTPS security.
Lesson goal
By the end of this lesson, you will understand how HTTP structures application-layer communication, the dynamic roles in the client-server model, remote procedure calls (RPCs), the anatomy of requests and responses, HTTP verbs and idempotency, status codes, and how HTTPS protects data in transit.
HTTP (Hypertext Transfer Protocol) is the foundational application-layer protocol used by web browsers, mobile apps, and backend services to exchange data, HTML documents, JSON payloads, images, and other resources across the World Wide Web.
While lower networking layers handle host addressing (IP) and reliable byte transmission (TCP), HTTP defines the vocabulary, structure, and rules that make distributed web communication possible.
The Client-Server Model
At the heart of HTTP is the client-server model, an architectural pattern where communication is organized into requests and fulfillment:
- The client initiates a request.
- The server receives, processes, and fulfills that request.
This model creates a clean separation of concerns: one machine asks for a resource or action, and another machine handles the computation and returns the result.
Dynamic server roles
A machine’s role is not permanently fixed by its hardware or hostname. Rather, its role is defined by the direction of each interaction:
Remember
The client initiates a request, and the server fulfills it. When servers communicate with one another, either server can take either role depending on who starts the exchange.
Remote Procedure Calls (RPC)
In complex distributed architectures, machines do more than just exchange static documents—they often need to invoke logic running on other machines across the network.
A Remote Procedure Call (RPC) is a mechanism that allows a program on one computer to execute code on another machine over a network as if it were a local function call.
Key idea
RPCs make code on remote machines executable across a network, using application-layer protocols to support and standardize the underlying communication.
HTTP's Position in the Networking Stack
To understand how HTTP carries both web traffic and RPCs, we must look at where HTTP sits in the layered network hierarchy.
HTTP operates at the Application Layer, positioned directly above the Transport Layer (TCP) and Network Layer (IP):
Because HTTP sits on top of TCP, it automatically inherits TCP's reliable, ordered stream delivery. HTTP does not need to worry about dropped packets or out-of-order bytes; instead, it focuses entirely on structuring requests and responses.
The request–response cycle
HTTP structures communication as a strict request-response model. A client sends a message containing request information, and the server returns a corresponding response message:
Each HTTP exchange is self-contained: it carries all necessary routing, metadata, and data required for the server to understand and process the client's intent.
Code sample
// [Path]
const response = await fetch("https://api.example.com/users", {
method: "POST", // [Method]
headers: { // [Headers]
"Content-Type": "application/json",
Authorization: "Bearer xyz",
},
body: JSON.stringify({ username: "alice" }), // [Body] ───> Server
});
const data = await response.json(); // [Body] <─── ServerRemember
HTTP sits above IP and TCP, structuring data into distinct, self-contained request-response pairs.
Inspecting HTTP Requests in Practice
Because HTTP is a structured protocol, every interaction can be observed directly in real time. Modern web browsers provide built-in developer tools that reveal every HTTP exchange generated by a page.
What browser DevTools provide
Browser developer tools allow you to:
- Observe all outbound HTTP requests made by a webpage (images, scripts, CSS, API calls).
- Identify the exact HTTP protocol version used (e.g.,
HTTP/1.1,h2for HTTP/2,h3for HTTP/3). - Inspect request and response headers, query parameters, payloads, and timing waterfalls.
Inspecting network activity step-by-step
- Open Developer Tools: Press
F12(orCmd + Option + Ion macOS) and navigate to the Network tab. - Generate Activity: Load or interact with a webpage. Each network request will appear as a row in the waterfall table.
- Identify Protocol Versions: Look at the Protocol column to see which HTTP version was negotiated.
- Examine Message Details: Click on any specific request row to inspect its request headers, response headers, status code, and response preview.
HTTP Message Anatomy
When you select an individual request in browser DevTools or inspect network traffic, what does the message look like? HTTP communication consists of two distinct message structures: Requests and Responses.
Request anatomy
An HTTP request includes four core components:
| Component | Purpose | Example |
|---|---|---|
| URL & Host | Identifies the destination domain and server. | https://developer.mozilla.org |
| Path | Identifies the specific resource location on the server. | /api/v1/whoami |
| Request Method | Describes the action or operation requested. | GET, POST, PUT, DELETE |
| Headers | Key-value pairs providing metadata about the client and request. | Accept: */*, Accept-Encoding: gzip, deflate, br, zstd, Accept-Language: en-US,en;q=0.7 |
| Body (Payload) | Optional data transmitted to the server (common in POST and PUT). | {"title": "New Post", "published": true} |
Response anatomy
An HTTP response includes:
| Component | Purpose | Example |
|---|---|---|
| Status Code & Line | Indicates the outcome of the request. | 200 OK, 404 Not Found, 500 Internal Server Error |
| Response Headers | Metadata describing the response and server configuration. | Content-Length: 181, Server: Google Frontend, X-Cache: MISS, MISS |
| Content Type | Specifies the MIME format of the returned data. | application/json |
| Body | The actual resource payload returned to the client. | HTML document markup, JSON object, image binary bytes |
Sequence for analyzing an HTTP exchange
When reading or debugging an HTTP exchange, follow this structured sequence:
- Request URL & Path: Determine where the request is directed.
- Request Method: Determine what action is being asked of the server.
- Request Headers: Check authentication tokens, accepted formats, and client metadata.
- Status Code: Verify whether the request succeeded or failed.
- Response Headers: Inspect the
Content-Type, caching policies, and cookies. - Response Body: Parse and validate the returned data payload.
Remember
A request describes what is being directed where; a response provides information about the result, including the status code and content type.
Routes, Endpoints, and HTTP Methods
While the URL path defines the route or endpoint (the location of the resource), the HTTP method (verb) defines the operation the server should perform on that resource.
By pairing the same endpoint with different methods, you can express full CRUD (Create, Read, Update, Delete) operations:
GET /api/users ──> Retrieve all users
POST /api/users ──> Create a new user
GET /api/users/42 ──> Retrieve user #42
PUT /api/users/42 ──> Update/replace user #42
PATCH /api/users/42 ──> Partially update user #42
DELETE /api/users/42 ──> Remove user #42The core HTTP methods
| Method | Server operation it represents | Primary use case |
|---|---|---|
GET | Retrieve information | Fetching a web page, reading a user profile, querying a list |
POST | Create or submit data | Submitting a form, creating a new account, placing an order |
PUT | Update / replace existing data | Overwriting an entire existing user record or document |
PATCH | Partially update existing data | Updating only a user's email without touching other fields |
DELETE | Remove information | Deleting a record, canceling a subscription, removing a file |
PUT vs PATCH
Both methods modify an existing resource, but they differ in scope:
PUTreplaces the entire resource. If your request body omits a field, that field is erased.PATCHapplies a partial update. Only the fields included in the body are changed; all other fields are left intact.
Choosing the right method
- Use
GETwhen reading state without causing side effects on the server. - Use
POSTwhen submitting new data that causes the server to create a resource or process an action. - Use
PUTwhen replacing an existing resource in its entirety with new data. - Use
PATCHwhen updating only specific fields of an existing resource. - Use
DELETEwhen deleting a target resource.
Remember
The endpoint indicates where the request is directed. The method indicates what kind of server operation is requested.
Request Bodies, Method Behavior, and Idempotence
Different HTTP methods behave differently with respect to data payloads, server state, caching, and retries.
Request bodies
A request body carries additional data sent with the request. Methods such as POST and PUT regularly include a body (e.g., JSON documents or form data) to provide the necessary payload for creation or updates. Conversely, GET and DELETE requests typically do not carry a body.
Understanding idempotence
A method is idempotent if making multiple identical requests has the same net effect on the server state as making a single request.
GET /item/1 (1x or 100x) ===> Server state remains unchanged (Idempotent)
PUT /item/1 (1x or 100x) ===> Server state set to the same value (Idempotent)
DELETE /item/1 (1x or 100x)===> Item 1 is removed (Idempotent)
POST /items (1x vs 10x) ===> Creates 1 item vs creates 10 items (NON-Idempotent)Idempotence is a critical concept in distributed system design:
- If a network glitch causes a
GET,PUT, orDELETErequest to time out, the client can safely retry the request automatically. - If a
POSTrequest times out, retrying blindly could result in duplicate database entries or double-charging a customer's credit card.
Key idea
The request body carries the data; the method determines how the server processes the data, whether the operation can be cached, and whether it is safe to retry (idempotence).
HTTP Status Codes
When a server finishes processing a request, it communicates the outcome back to the client using a three-digit HTTP status code.
Status codes are grouped into five standard ranges:
1xx(Informational): Request received, continuing process.2xx(Success): The action was successfully received, understood, and accepted.3xx(Redirection): Further action must be taken to complete the request (e.g., URL moved).4xx(Client Error): The request contains bad syntax or cannot be fulfilled by the client.5xx(Server Error): The server failed to fulfill an apparently valid request.
Common status codes
| Code | Name | Meaning | Common Scenario |
|---|---|---|---|
200 | OK | Request succeeded | Standard response for successful GET or PUT operations. |
201 | Created | Resource created | Returned by POST when a new record is created in the database. |
204 | No Content | Succeeded with no body | Common for successful DELETE requests where no data needs to return. |
400 | Bad Request | Client error / invalid syntax | Request body missing required fields or containing invalid JSON. |
401 | Unauthorized | Missing authentication | Client must log in or provide a valid API token. |
403 | Forbidden | Lacks permissions | Client is authenticated, but does not have permission for the resource. |
404 | Not Found | Resource missing | The requested URL path does not match any resource on the server. |
500 | Internal Server Error | Server failure | An unhandled exception or crash occurred in the backend code. |
502 | Bad Gateway | Upstream server failure | An API gateway or reverse proxy received an invalid response from upstream. |
503 | Service Unavailable | Server overloaded / down | The server is temporarily unable to handle requests due to maintenance or load. |
When reading an HTTP response, immediately check the status code range to pinpoint who owns the problem: a 4xx code means the client needs to change its request; a 5xx code means the backend server encountered an internal failure.
Securing the Channel: HTTPS, SSL, and TLS
Plain HTTP transmits all message contents—URLs, headers, authentication cookies, authorization tokens, and request/response bodies—as unencrypted plaintext. Anyone positioned on the physical network path between the client and server (such as an insecure public Wi-Fi network, an ISP, or a compromised router) can inspect or alter the traffic.
HTTPS (Hypertext Transfer Protocol Secure) eliminates this vulnerability by wrapping standard HTTP inside an encrypted security layer using TLS (Transport Layer Security), historically known as SSL (Secure Sockets Layer).
┌───────────────────────────────────────────────────────────┐
│ HTTP Application Data (Plaintext) │
├───────────────────────────────────────────────────────────┤
│ TLS / SSL Encryption & Authentication Layer │
├───────────────────────────────────────────────────────────┤
│ TCP Transport Layer │
└───────────────────────────────────────────────────────────┘The relationship between HTTP, TLS, and HTTPS
- HTTP defines what is being transmitted (requests and responses).
- TLS / SSL is the cryptographic protocol that encrypts and secures the communication.
- HTTPS is standard HTTP data running through a secure TLS connection:
TLS handshake
Before the first HTTP byte is sent over an HTTPS connection, the client and server run a TLS handshake to negotiate a shared encryption key:
1. Client Hello ──> Client sends supported TLS versions & cipher suites
2. Server Hello <── Server selects version & cipher; sends its certificate
3. Key Exchange ──> Client verifies the certificate, derives a session key
4. Finished ──> Both sides confirm the handshake; encrypted channel openKey Takeaways
- Client-Server Architecture: Clients initiate requests; servers fulfill them. In distributed architectures, backend servers frequently act as clients to other servers.
- Remote Procedure Calls (RPC): RPC frameworks allow machines to execute code across a network using application-layer protocols.
- Protocol Layering: HTTP sits at the application layer directly above TCP and IP, structuring data into discrete request-response exchanges.
- Message Anatomy: Requests specify a URL, path, method, headers, and optional body; responses return a status code, response headers, content type, and body.
- HTTP Methods & Idempotence:
GET,PUT, andDELETEare idempotent (safe to retry);POSTis non-idempotent (creates new state on each execution);PATCHpartially updates a resource without replacing the whole record. - Status Codes:
2xxindicates success,3xxredirection,4xxclient errors, and5xxserver failures. - HTTPS Security: HTTPS wraps HTTP in TLS encryption, providing confidentiality, data integrity, and server authentication.