HTTP/1.1 semantics
Read and write raw HTTP requests and responses including methods, status codes and headers.
By Invicly Tech, , in Computer Networking. About 11 minutes to read.
In short
HTTP is a request-response protocol: the client sends a method, path, headers and optional body, and the server replies with a status code, headers and body. HTTP/1.1 is plain text, so you can read it directly with curl -v. Headers control caching, content type, cookies, authentication and connection reuse.
Explained simply
HTTP (Hypertext Transfer Protocol) is how browsers and apps ask servers for things. The client sends a request: what it wants to do (the method, like GET), which thing (the path, like /products), and some extra labeled facts (headers). The server sends back a response: a three-digit status code, its own headers, and usually a body such as a web page or JSON data.
HTTP/1.1 is plain text, so you can read it with your own eyes. Each line ends with a carriage return and line feed, the headers end with one empty line, and then the body begins. This makes it a great protocol to learn on, and tools like curl -v show you every line.
HTTP itself forgets you after every request. To remember who you are, servers send a cookie: a small piece of text the browser stores and sends back with later requests. Logins, shopping carts and language choices all ride on cookies.
Headers also control caching. A server can say: keep this file for one hour, or check with me first. Good caching headers are one of the cheapest ways to make a site faster, because the fastest request is the one never sent.
Press Play on a diagram to walk through it one step at a time.
- Browser to Web server: TCP handshake (and TLS for HTTPS)1TCP handshake (and TLS for HTTPS)
- Browser to Web server: GET /index.html HTTP/1.1, Host: example.com2GET /index.html HTTP/1.1, Host: example.com
- Web server to Browser: HTTP/1.1 200 OK, Content-Type: text/html3HTTP/1.1 200 OK, Content-Type: text/html
- Browser to Web server: GET /style.css on the same keep-alive connection4GET /style.css on the same keep-alive connection
- Web server to Browser: 304 Not Modified (cached copy still valid)5304 Not Modified (cached copy still valid)
- 1xx informational
- 100 Continue
- 101 Switching Protocols
- 2xx success
- 200 OK
- 201 Created
- 204 No Content
- 3xx redirection
- 301 and 308 permanent
- 302 and 307 temporary
- 304 Not Modified
- 4xx client error
- 400 Bad Request
- 401 and 403
- 404 Not Found
- 429 Too Many Requests
- 5xx server error
- 500 Internal Server Error
- 502 Bad Gateway
- 503 Service Unavailable
- 504 Gateway Timeout
In depth
The exact shape of a message
A request starts with the request line: method, target and version, such as GET /search?q=dns HTTP/1.1. Header lines follow, each Name: value. The Host header is required in HTTP/1.1. A blank line ends the headers. If a body follows, the receiver must know where it ends, and there are two ways to say so. Content-Length gives the size in bytes. Transfer-Encoding: chunked sends the body in pieces, each prefixed by its size in hexadecimal, ending with a zero-size chunk; servers use this when they do not know the size in advance, such as streamed output.
A response starts with a status line, such as HTTP/1.1 404 Not Found, followed by headers, a blank line and the body. Header names are not case-sensitive. Connections stay open by default in HTTP/1.1 (keep-alive); Connection: close asks to close after this response.
- 1Request line
POST /api/orders HTTP/1.1
- 2Headers
Host, Content-Type, Content-Length, Cookie
- 3Blank line
A lone CRLF marks the end of headers
- 4Body
{"item": 42, "qty": 1}
Choosing methods and status codes
Methods carry meaning that caches, proxies and retries rely on. GET and HEAD are safe: they should never change data. PUT and DELETE are idempotent: sending them twice has the same effect as once, so a client may retry after a timeout. POST is neither, which is why browsers warn before resubmitting a form. PATCH applies a partial change. OPTIONS asks what is allowed and is used by browsers for CORS preflight checks.
Pick the status code that tells the truth. 201 Created for a new resource, with a Location header. 204 No Content when there is nothing to return. 301 and 308 are permanent redirects, 302 and 307 temporary; 307 and 308 keep the method and body, while browsers turn a 301 or 302 POST into a GET. 401 means not logged in, 403 means logged in but not allowed, 409 means a conflict, 429 means slow down. 502 and 504 come from a proxy that could not get a good or timely answer from the app behind it.
- 401: who are you? 403: I know you, and no
- 502: bad answer from upstream; 504: no answer in time
- 503 with Retry-After: temporarily overloaded or in maintenance
How caching decides
Cache-Control is the main header. max-age=3600 lets any cache reuse the response for an hour without asking. s-maxage sets a different time for shared caches such as CDNs. private allows only the browser to store it, not shared caches. no-cache, despite its name, allows storing but requires checking with the server before every reuse. no-store forbids storing at all and is right for sensitive data.
Checking with the server is called revalidation. The server gives each version an ETag (a fingerprint) or a Last-Modified date. The client later sends If-None-Match with the ETag, or If-Modified-Since with the date. If nothing changed, the server replies 304 Not Modified with no body, saving bandwidth. A common pattern: put a content hash in file names like app.3f9a1c.js and serve them with max-age=31536000, immutable; serve the HTML with no-cache so new deploys show up at once.
- 1Find a stored copy
Saved with its ETag and max-age
- 2Still fresh?
Yes: use it, no request at all
- 3Stale: revalidate
Send If-None-Match with the ETag
- 4304 Not Modified
Reuse the copy, refresh its age
- 5200 OK
Changed: replace the stored copy
Cookies and their safety flags
A server sets a cookie with a response header such as Set-Cookie: session=abc123; Path=/; Max-Age=3600; HttpOnly; Secure; SameSite=Lax. The browser then sends Cookie: session=abc123 on matching requests. Each attribute matters. Max-Age or Expires makes the cookie survive a browser restart; without them it is a session cookie. Domain widens the cookie to subdomains, so leave it out unless you need that. HttpOnly hides the cookie from JavaScript, which limits damage from cross-site scripting. Secure sends it only over HTTPS.
SameSite controls cross-site sending. Strict never sends it on requests started by other sites. Lax, the default in modern browsers when the attribute is missing, sends it on normal top-level navigation but not on cross-site form posts or embedded requests, which blocks most CSRF attacks. None sends it everywhere but must be combined with Secure.
Connections and head-of-line blocking
In HTTP/1.1, one TCP connection carries one request at a time: send a request, wait for the full response, then send the next. Pipelining (sending several requests without waiting) exists in the standard but responses must come back in order, and it broke so many proxies that browsers turned it off. So a slow response blocks everything queued behind it on that connection, which is called head-of-line blocking.
Browsers work around it by opening about six parallel connections per host. Old performance tricks came from this limit: joining files into bundles, image sprites, and spreading assets across several domains (domain sharding). HTTP/2 removes the need for most of them. When you debug, remember that a proxy or load balancer may keep its own pool of connections to the backend, separate from the client's connection.
Learn it in 4 short steps
About 40m each. Track them with a timer in your plan.
- 1Read request line, headers and body~40m
- 2Use methods and status codes correctly~40m
- 3Explain caching and cookies headers~40m
- 4Send raw requests with curl -v~40m
Key points
- Status classes: 2xx success, 3xx redirect, 4xx client error, 5xx server error.
- GET, HEAD, PUT and DELETE are idempotent; POST is not.
- The Host header lets many sites share one IP address.
- Cache-Control, ETag and Last-Modified let clients reuse responses.
Key terms
Tap a card to flip it. 0/5 known
Examples
curl -v http://example.com/ -o /dev/null
# > GET / HTTP/1.1
# > Host: example.com
# < HTTP/1.1 200 OK
# < Content-Type: text/html
printf 'HEAD / HTTP/1.1\r\nHost: example.com\r\nConnection: close\r\n\r\n' | nc example.com 80Testing an API by hand with curl
You want to create an order and check that the API answers correctly. Step 1: POST JSON with the right Content-Type. Step 2: read the status (201) and the Location header. Step 3: GET the new resource. -i prints response headers with the body, -H adds a header, and -d sends a body (and makes curl use POST). The -w option prints the status code alone, which is handy in scripts.
curl -i -X POST https://api.example.com/orders \
-H 'Content-Type: application/json' \
-H 'Authorization: Bearer TOKEN' \
-d '{"item": 42, "qty": 1}'
# HTTP/1.1 201 Created
# Location: /orders/981
curl -s -o /dev/null -w '%{http_code}\n' https://api.example.com/orders/981
# 200Checking that caching really works
After a deploy, you want to confirm the CSS file is cached and revalidated properly. Step 1: fetch headers with -I and note Cache-Control and ETag. Step 2: send the same request with If-None-Match set to that ETag. A 304 response proves revalidation works. Step 3: if you get 200 every time, the server may be generating a new ETag per request, or a proxy strips the header.
curl -sI https://example.com/app.css | grep -iE 'cache-control|etag'
# cache-control: max-age=3600
# etag: "5f2a-63c1"
curl -sI https://example.com/app.css \
-H 'If-None-Match: "5f2a-63c1"' | head -1
# HTTP/1.1 304 Not ModifiedQuick reference
- curl -v shows request and response headers; -I sends HEAD; -i adds headers to the output
- curl -L follows redirects; --resolve host:443:IP tests a server before DNS points to it
- Safe: GET, HEAD, OPTIONS. Idempotent: those plus PUT and DELETE. POST is neither
- 307 and 308 keep the method on redirect; 301 and 302 may turn POST into GET
- no-cache = revalidate every time; no-store = never store
- Hashed file names + max-age=31536000, immutable; HTML with no-cache
- Session cookies: HttpOnly; Secure; SameSite=Lax as a sane default
- Body length: Content-Length or Transfer-Encoding: chunked
Common mistakes
- Returning 200 with an error message in the body instead of a proper status.
- Using GET for actions that change data.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Use curl -v on any website and write down the request line, the Host header, the status line and three response headers.
- Exercise 2medium
Send a complete HTTP/1.1 request by hand over a raw TCP connection and read the response, including where the headers end.
- Exercise 3hard
Find a redirect chain (for example http to https, then to www) and show each hop's status code and Location, then measure how much time the redirects cost.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- What is the difference between 401 and 403?
- 401 Unauthorized means the server does not know who you are, so log in or send credentials. 403 Forbidden means it knows who you are and you are not allowed.
- Is HTTP/1.1 still used?
- Yes. Many APIs, internal services, proxies to backends and simple tools still use it, and every newer version keeps the same methods, headers and status codes.
- What is the difference between PUT and PATCH?
- PUT replaces the whole resource with what you send, so it is idempotent. PATCH sends only the changes, and whether it is idempotent depends on the patch format.
- Why does my browser show an old version of my site after a deploy?
- The files were cached with a long max-age. Use hashed file names for assets and serve the HTML with no-cache, so the browser always checks for a new page.