HTTP
The shared language clients and servers use to talk over the web.
What is it?
A client and server must agree on a format for requests and responses, or neither understands the other. HTTP (HyperText Transfer Protocol) is that shared language. It sets how a request is structured, what kinds of requests exist, and how a server should respond.
Explain like I'm 10
HTTP is like the standard format for a letter: return address, recipient, subject, body. Because everyone agrees on it, any recipient (server) can read a well-formed letter (request) and is expected to reply in the same format (a response).
Examples
A basic HTTP request/response
GET /users/1 HTTP/1.1
Host: api.example.com
// Server responds:
HTTP/1.1 200 OK
Content-Type: application/json
{ "id": 1, "name": "Amara" }GET /users/1 asks for user #1. The server replies with a status code (200 OK means success) and the requested data.
How it works
Every request has a method (the action, like GET or POST), a path (the resource it's about), and optional headers and body (extra data).
Every response has a status code (like 200 for success or 404 for not found) plus its own headers and body.
Why does it exist?
Without a shared protocol, every client and server pair would need its own custom way to talk, and nothing on the web would work together. HTTP gives every browser, app, and server a common language.
When to use it
You use HTTP any time a browser, app, or script talks to a web server, which covers most client-server communication.
When not to use it
For very frequent, low-latency messages (real-time games, some trading systems), HTTP's overhead can be too much. WebSockets or dedicated binary protocols fit better there.
Common mistakes
Mixing up status code groups, e.g. thinking 4xx means 'server error'. 4xx is a client error; 5xx is a server error.
Forgetting HTTP is stateless by default. The server doesn't remember earlier requests unless something (like cookies or tokens) carries that state.
Using the wrong method, e.g. GET for something that changes data on the server.
Practice exercises
- Easy:
List the HTTP methods GET, POST, PUT, and DELETE, and describe what each is usually used for.
- Medium:
Look up what HTTP status codes 200, 301, 404, and 500 mean.
- Hard:
Explain in your own words why HTTP being 'stateless' matters for how servers scale.
Interview questions
What is HTTP?
A protocol (a set of rules) for how clients and servers structure requests and responses on the web.
What does it mean that HTTP is stateless?
Each request is handled on its own. The server doesn't remember earlier requests from the same client unless something extra (cookies, tokens, sessions) is used.
What's the difference between GET and POST?
GET reads data without changing anything on the server, and can be cached. POST usually sends data to create or change something.
What does it mean for an HTTP method to be "idempotent"? Which common methods are idempotent?
Repeating the same request gives the same end result. GET, PUT, DELETE, HEAD, and OPTIONS are idempotent; POST and PATCH generally are not.
What does it mean for an HTTP method to be "safe", and how is that different from being idempotent?
A safe method doesn't change server state at all (read-only, like GET or HEAD). Idempotent only means repeats don't change the outcome further. So DELETE is idempotent but not safe: it changes state on the first call, not on repeats.
Why is PUT considered idempotent but POST is not?
PUT replaces a resource with exactly what you send, so sending it twice leaves the same final state. POST usually creates a new resource each time, so repeating it (e.g. resubmitting a form) can create duplicates.
Is DELETE idempotent? Explain with an example.
Yes. Deleting a resource twice leaves it in the same state (gone) as deleting it once. The second call may return 404 instead of success, but the result on the server is the same, and that's what idempotency measures.
What's the difference between PATCH and PUT?
PUT replaces the whole resource with the payload. PATCH is a partial update: it changes only the fields you send and leaves the rest alone.
What are the five broad categories of HTTP status codes?
1xx (informational, request received and being processed), 2xx (success), 3xx (redirection, further action needed), 4xx (client error), and 5xx (server error).
What's the difference between a 401 and a 403 status code?
401 Unauthorized: the request lacks valid authentication (credentials missing or invalid). 403 Forbidden: the client is identified but isn't allowed to access that resource.
What's the difference between a 404 and a 410 status code?
404 Not Found: nothing is at that URL, possibly temporarily. 410 Gone: something used to be there and was permanently removed, a stronger and more specific signal.
What's the difference between a 500 and a 503 status code?
500 Internal Server Error: something broke while the server handled the request (an unhandled error, a bug). 503 Service Unavailable: the server is temporarily unable to respond (overloaded, maintenance), so the client can retry later.
What's the difference between a 301 and a 302 redirect?
301 Moved Permanently: the resource has a new URL for good, and future requests (and search engines) should go there. 302 Found: a temporary redirect, so the client should keep using the original URL.
Why does it matter whether a redirect is 301 or 302 for things like caching or search engine indexing?
Because a 301 is permanent, browsers and search engines may cache it and move the old URL's search ranking to the new one. A 302 isn't treated that way, since the original URL is expected to work again.
What is the purpose of HTTP headers? Give an example of a request header and a response header.
Headers carry metadata separate from the body. The request header Host tells the server which website the request is for; the response header Content-Type tells the client how to read the body.
What's the difference between the Content-Type and Accept headers?
Content-Type (sent by whichever side includes a body) describes the format of the data being sent. Accept (sent by the client) lists the response formats it will take, so the server can choose.
At a high level, how does HTTP caching work, and which headers control it?
A response can include headers like Cache-Control (how long it may be reused, and by whom). A client or proxy can then reuse a stored copy instead of asking again, until it expires or is invalidated.
What's the difference between Cache-Control: no-cache and Cache-Control: no-store?
no-cache allows storing a response but requires checking with the server before each reuse. no-store forbids storing it at all, for sensitive data that should never be cached.
What is an ETag, and how does it help avoid re-downloading unchanged data?
A token the server gives to one version of a resource. The client sends it back later in If-None-Match; if nothing changed, the server replies 304 Not Modified with no body, saving bandwidth.
What's the purpose of the If-None-Match header, and how does the server respond to it?
It tells the server 'send the full response only if the ETag changed since this value'. If the ETag still matches, the server returns 304 Not Modified; otherwise it returns the current version with 200 OK.
Why is HTTP described as stateless, and what mechanisms let applications build state (like being logged in) on top of it?
Each request is handled on its own, with no built-in memory of earlier ones. Apps add state with cookies, sessions, or tokens sent on every request to identify the client.
At a conceptual level, what changed between HTTP/1.1 and HTTP/2?
HTTP/1.1 generally needs a separate connection per concurrent request, or queues requests on one. HTTP/2 multiplexes many requests and responses over one connection and compresses headers, cutting overhead and avoiding head-of-line blocking at the connection level.
What problem does HTTP keep-alive solve?
Without it, a new TCP connection would be opened and closed for every request, which is slow. Keep-alive reuses one connection for many requests and responses.
Why is the Host header required, even though the request is already being sent to a specific server's IP address?
One IP address (and port) can serve many websites (virtual hosting). The server needs Host to know which site the request is for.
Why might a client send a HEAD request instead of a GET?
HEAD returns the same headers as GET but no body. A client can check whether a resource exists or how large it is without downloading it.
What is the OPTIONS method typically used for?
It asks the server which methods and headers a resource allows. Browsers most often send it automatically as a CORS "preflight" before a cross-origin request that isn't "simple".
What's the difference between a query parameter and a path parameter in a request URL?
A path parameter identifies a resource as part of the URL (e.g. /users/42). A query parameter (e.g. ?sort=name&page=2) changes how it's fetched (filtering, sorting, pagination) without changing which resource it is.
Why is it a mistake to use GET for a request that changes data on the server?
GET is expected to be safe and idempotent, so browsers, proxies, and crawlers may prefetch, cache, or repeat it freely. If GET has side effects, those tools can trigger changes nobody intended.
What is content negotiation in HTTP?
How a client and server agree on the best representation to return. For example, the client's Accept header lists preferred formats or languages, and the server picks a match.
Why does the 429 status code exist, and how should a well-behaved client respond to it?
429 Too Many Requests means the client is rate-limited. A good client backs off and retries later, respecting a Retry-After header if present, instead of retrying at once and making things worse.