Learn how HTTP requests and responses work, what methods and status codes tell you, when a retry is safe, and what HTTPS protects.
HTTP (Hypertext Transfer Protocol) defines how clients and servers exchange requests and responses. A request uses a method such as GET or POST to describe the requested action. The response uses a status code such as 200 OK or 201 Created to describe the outcome. These meanings are defined in RFC 9110, HTTP Semantics.
Lesson goal
Inspect a request and its response, explain what HTTPS protects, and decide when repeating a request is safe.
Before you begin — DNS.
After resolving a hostname and opening a connection, a browser can use HTTP to ask for a page or submit data. HTTP/1.1 and HTTP/2 commonly use TCP. HTTP/3 uses QUIC over UDP.
Suppose an app wants to create a user. It sends POST /users to the API, sets Content-Type: application/json, and includes the user details in the body. The server creates the record and replies with 201 Created. Its response can include a Location header identifying the new resource and a body containing the representation.
In this exchange the app is the client because it initiated the request. If the API calls another service to finish the work, the API is a client in that second exchange.
const response = await fetch("https://api.example.com/users", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ username: "alice" }),
});
console.log(response.status, response.headers.get("Location"));The hostname in these examples is illustrative. To study a real exchange, open the Network panel in your browser’s developer tools and reload a page. Select a request and read its target and method, the request headers and optional payload, then the response status, headers, and body. A 204 No Content response ends after its headers.
| Method | What the client requests | Example |
|---|---|---|
GET | Retrieve a representation. | Read /users/42. |
POST | Ask the target to process the submitted data. | Create a user through /users. |
PUT | Create or replace the state at a particular URI, if allowed by the API. | Store the complete profile at /users/42. |
PATCH | Apply the supplied changes to the target. | Set one field on the profile. |
DELETE | Remove the association between the target URI and its current functionality. | Remove access to /users/42. |
An HTTP status code describes the outcome of this exchange. 1xx reports interim progress, 2xx success, 3xx redirection or cache-related outcomes, 4xx a problem fulfilling this request, and 5xx a server-side failure. Read the specific code. A 404 says the target was not found or its existence was withheld. A 503 says the service is temporarily unavailable. Neither tells you exactly what happened inside a database. HTTP semantics
The server may finish a request even when the client never receives the reply. A method is idempotent when repeating an identical request has the same intended effect as sending it once. GET, PUT, and DELETE have this property. Two identical DELETE calls can receive different responses, such as a successful deletion followed by 404, while leaving the resource in the same final state.
A POST /users that creates a user on every call could create two users after a retry. For PATCH, inspect the operation. Setting a field to a fixed value can be idempotent. Incrementing it by one changes the result each time. The API contract must tell the client how to handle an uncertain outcome.
HTTPS uses Transport Layer Security (TLS) to authenticate the server and protect HTTP data in transit from disclosure or alteration. During a typical first connection, the client checks the server’s certificate and the peers establish keys for protected traffic. The receiving service can then read the request and apply its own access rules.
A client sends a complete profile with PUT /users/42. The connection fails before it receives a response. What does the failure tell the client, and what does the method tell it?
The connection failure leaves the outcome unknown. If the API implements PUT as specified, resending the same representation asks for the same final state. The first attempt may already have succeeded, so the two response codes could differ.