Lesson 7 of 14 · 10 min · HTTP, TLS and traffic management
HTTP: requests, methods, status codes and headers
What you will learn
- The anatomy of an HTTP request and response
- Methods (GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS) and the ideas of safe and idempotent
- Status codes by class and the ones you must know
- Important headers: Host, Content-Type, Authorization, Cache-Control, cookies
- HTTP versions 1.1, 2 and 3
- Using curl to see everything
In simple words
HTTP is the language that browsers and servers speak. The browser sends a request ("please give me this page") and the server sends back a response with a three-digit number that says how it went. Once you can read one request and one response, most web problems become easier to understand.
Words to know
- Request: the message a client sends, such as "GET /pricing".
- Method: the action asked for: GET reads something, POST sends something, DELETE removes something.
- Status code: the three-digit result: 200 is OK, 404 is not found, 500 is a server error.
- Header: extra information sent with a request or response, such as the type of content.
HTTP in one picture
HTTP is a request-response protocol: the client sends a request, the server sends back one response. It is stateless: the server treats each request on its own, which is why cookies and tokens exist to carry identity. A request:
GET /products?id=42 HTTP/1.1
Host: shop.example.com
Accept: application/json
Authorization: Bearer eyJhbGciOi...
and a response:
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 58
Cache-Control: max-age=60
{"id": 42, "name": "Notebook", "price": 149}
A request has a method, a target (path and query), the version, headers, and an optional body. A response has a status code, headers and an optional body. Headers are Name: value lines; a blank line separates headers from the body.
Methods
| Method | Meaning | Body | Safe | Idempotent |
|---|---|---|---|---|
| GET | read a resource | no | yes | yes |
| HEAD | like GET but headers only | no | yes | yes |
| POST | create something / run an action | yes | no | no |
| PUT | replace a resource completely | yes | no | yes |
| PATCH | change part of a resource | yes | no | no (not guaranteed) |
| DELETE | remove a resource | rarely | no | yes |
| OPTIONS | ask what is allowed (CORS preflight) | no | yes | yes |
Safe means it does not change server state, so it can be called freely by crawlers, caches and prefetching. Idempotent means doing it many times has the same effect as doing it once: DELETE /orders/7 twice leaves the same end state, while POST /orders twice creates two orders. That is why a client may retry a failed GET or PUT automatically but must be careful retrying a POST (a payment API uses an idempotency key header for this).
Servers decide which methods a path supports. Python's tiny test server shows what an unsupported method looks like:
curl -X OPTIONS http://127.0.0.1:8080/ -> HTTP/1.0 501 Unsupported method ('OPTIONS')
curl http://127.0.0.1:8080/nope -> HTTP/1.0 404 File not found
curl -X DELETE http://127.0.0.1:8080/ -> HTTP/1.0 501 Unsupported method ('DELETE')
Status codes
The first digit gives the class:
| Class | Meaning | Codes to know |
|---|---|---|
| 1xx informational | 101 Switching Protocols (WebSocket upgrade) | |
| 2xx success | it worked | 200 OK, 201 Created, 204 No Content |
| 3xx redirection | go elsewhere | 301 Moved Permanently, 302/307 temporary redirect, 304 Not Modified (use your cached copy) |
| 4xx client error | the request is wrong | 400 Bad Request, 401 Unauthorized (not authenticated), 403 Forbidden (authenticated but not allowed), 404 Not Found, 405 Method Not Allowed, 408 timeout, 409 Conflict, 413 payload too large, 429 Too Many Requests |
| 5xx server error | the server failed | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable, 504 Gateway Timeout |
The distinctions interviewers ask about:
- 401 vs 403. 401 means "I do not know who you are" (missing or bad credentials). 403 means "I know who you are and you may not do this".
- 301 vs 302. 301 is permanent (browsers and search engines remember it); 302 and 307 are temporary. 307/308 keep the method; 301/302 historically let clients change POST to GET.
- 502 vs 503 vs 504. From a proxy or load balancer: 502 the backend gave an invalid answer or the connection failed; 503 the service is overloaded or has no healthy backends; 504 the backend did not answer in time.
- 5xx are your fault, 4xx are usually the caller's. Alert on a rising 5xx rate.
Headers that matter
| Header | Direction | Purpose |
|---|---|---|
Host |
request | which site, so one IP can serve many domains (virtual hosting). Required in HTTP/1.1 |
User-Agent |
request | who is calling |
Accept, Content-Type |
both | media types wanted / sent (application/json, text/html) |
Authorization |
request | credentials: Bearer <token>, Basic <base64> |
Cookie / Set-Cookie |
request / response | session state; flags HttpOnly, Secure, SameSite protect it |
Cache-Control, ETag, Last-Modified |
response | caching rules and validators |
Location |
response | the target of a redirect |
Content-Length, Transfer-Encoding |
both | how big the body is |
X-Forwarded-For, X-Forwarded-Proto, Forwarded |
request | added by proxies: the original client IP and scheme |
Strict-Transport-Security |
response | tells browsers to use HTTPS only |
Caching in two lines
Cache-Control: max-age=60 lets caches reuse the response for 60 seconds. Cache-Control: no-store forbids storing. With ETag, a client revalidates by sending If-None-Match; if unchanged, the server answers 304 without a body. Good caching makes sites faster and cheaper; wrong caching of private pages is a security bug (use private or no-store for user-specific content).
HTTP versions
- HTTP/1.1: text-based, one request at a time per connection (browsers open several connections); keep-alive reuses connections.
- HTTP/2: binary, many requests multiplexed on one connection, header compression. Used over TLS.
- HTTP/3: like HTTP/2 but over QUIC (UDP), so one lost packet does not stall every stream.
curl -sI https://example.com -o /dev/null -w '%{http_version}' printed 2 in our test.
Using curl as your HTTP lab
curl -i https://example.com/ # response headers and body
curl -I https://example.com/ # HEAD request: headers only
curl -v https://example.com/ -o /dev/null # the whole exchange (> sent, < received)
curl -X POST -H 'Content-Type: application/json' -d '{"name":"x"}' https://api.example.com/items
curl -L http://github.com -o /dev/null -w 'redirects=%{num_redirects} final=%{url_effective}\n'
curl -H 'Authorization: Bearer TOKEN' https://api.example.com/me
curl -sS -o /dev/null -w '%{http_code}\n' https://example.com/ # just the status
Real result of the redirect test: redirects=1 final=https://github.com/ (the plain-HTTP URL redirects to HTTPS). -f makes curl return a failure exit code for 4xx/5xx, which is what you want in scripts. Do not put secrets in commands you share or log; -v prints headers including Authorization.
REST in short
Most APIs follow REST conventions: resources have URLs (/orders/7), methods express the action (GET read, POST create, PUT replace, DELETE remove), status codes report the outcome, and bodies are JSON. Know the vocabulary: endpoint, payload, pagination, versioning (/v1/), rate limiting (429 with Retry-After).
Common mistakes
- Returning 200 with an error message in the body. Monitoring and clients rely on status codes.
- Confusing 401 and 403.
- Retrying non-idempotent requests blindly.
- Caching authenticated pages publicly.
- Forgetting the
Hostheader when testing by IP:curl -H 'Host: site.example' http://IP/. - Putting secrets in URLs (they end up in logs); use headers or bodies.
Try it yourself
- Use
curl -vagainst a website and mark the request line,Host, response status and three response headers. - Find one site that returns each of: 301, 404, 405 (try
curl -X DELETE). Explain each. - Send a POST with JSON to
https://httpbin.org/postor your own test server and read what comes back. - Add
Cache-Controlheaders in nginx for static files and verify withcurl -I.
Check yourself
What is the difference between 401 and 403?
401: the client is not authenticated (or credentials are wrong). 403: the client is authenticated but does not have permission.
What does idempotent mean, and which methods are idempotent?
Repeating the request has the same effect as sending it once. GET, HEAD, PUT, DELETE and OPTIONS are idempotent; POST generally is not.
What do 502, 503 and 504 mean from a load balancer?
502: bad or failed response from the backend; 503: service unavailable or no healthy backends; 504: the backend did not respond within the timeout.
Why is HTTP called stateless, and how do sites keep users logged in?
Each request carries all the information the server needs; the server keeps no memory between requests by default. Sessions use cookies or tokens sent with every request.
Interview questions
What is the difference between PUT and PATCH?
PUT replaces the entire resource with the body; PATCH applies a partial change. PUT is idempotent by definition; PATCH may not be.
What does a 304 response mean?
Not Modified: the client's cached copy is still valid, so the server sends no body, saving bandwidth. It comes from conditional requests with If-None-Match or If-Modified-Since.
How do HTTP/2 and HTTP/3 improve on HTTP/1.1?
HTTP/2 multiplexes many requests on one connection and compresses headers. HTTP/3 runs over QUIC/UDP, removing TCP's head-of-line blocking and speeding connection setup.
Summary
- HTTP is stateless request-response: method + path + headers + body in; status + headers + body out.
- Know safe/idempotent methods and the status classes; 401 vs 403 and 502/503/504 are classic questions.
- Key headers: Host, Authorization, Content-Type, Cache-Control, Set-Cookie, X-Forwarded-*.
curl -v,-I,-ware the quickest way to understand a web problem.
Going deeper
- RFC 9110: HTTP Semantics: methods, status codes and headers, authoritative.
- RFC 9111: HTTP Caching: the caching rules.
- MDN: HTTP: friendly reference for headers, methods and status codes.
See how much of this you can explain
A free mock interview asks you new questions on Networking for DevOps for DevOps Engineer and scores every answer, with what to fix.