Sub topic 14
HTTP Methods, Status Codes, and Common Headers
Prerequisites and working model
Assume you know the layered network model, TCP connections, sockets, and the meaning of latency, bandwidth, and round-trip time. HTTP is an application-layer protocol. It sends requests and responses as messages over a transport such as TCP. HTTP/1.1 commonly uses a persistent TCP connection, while HTTP/3 uses QUIC over UDP.
An HTTP request asks a server to perform an operation on a resource. A response reports the result. The method describes the intended operation, the status code describes the result, and headers carry metadata.
GET /notes/7 HTTP/1.1
Host: example.com
Accept: application/json
Connection: keep-alive
The first line is the request line. It contains the method, target path, and HTTP version. Headers follow as name-value pairs. A blank line separates headers from an optional body.
Methods and their properties
The most tested methods are:
| Method | Typical meaning | Safe | Idempotent |
|---|---|---|---|
| GET | Retrieve a resource | Yes | Yes |
| POST | Create or process data | No | No |
| PUT | Replace a resource | No | Yes |
| PATCH | Partially modify a resource | No | No |
| DELETE | Remove a resource | No | Yes |
| HEAD | Get headers without the response body | Yes | Yes |
A method is safe when it is intended not to change server state. A method is idempotent when repeating the same request has the same intended effect as sending it once. Idempotence does not mean that every response is identical. For example, sending DELETE twice might return 204 first and 404 second, but the resource is absent after either completed operation.
POST is often used when the server chooses a new resource identifier. PUT usually targets a known identifier, such as /users/12, and replaces that resource. These are semantic conventions, not guarantees about a particular implementation.
Status codes
The first response line contains the HTTP version, a three-digit status code, and a reason phrase.
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 16
{"id":7,"ok":true}Status-code classes are:
1xx: informational.2xx: successful processing, such as200 OK,201 Created, and204 No Content.3xx: redirection or cache-related instructions, such as301 Moved Permanently,302 Found, and304 Not Modified.4xx: a client-side problem, such as400 Bad Request,401 Unauthorized,403 Forbidden,404 Not Found, and405 Method Not Allowed.5xx: a server-side failure, such as500 Internal Server Error,502 Bad Gateway, and503 Service Unavailable.
A 401 response indicates that authentication credentials are required or invalid. A 403 response means the server understood the request but refuses to authorize it. This distinction is a common conceptual question.
Headers that control interpretation
Common request headers include Host, Accept, Authorization, Cookie, Content-Type, and Content-Length. Common response headers include Content-Type, Content-Length, Location, Set-Cookie, Cache-Control, ETag, and Last-Modified.
Content-Type describes the representation in the message body. Accept states which response formats the client prefers. Location commonly identifies the target of a redirect or the resource created by a 201 response.
def status_class(code):
if not 100 <= code <= 599:
raise ValueError("status code must be three digits")
return f"{code // 100}xx"
for code in (200, 301, 404, 503):
print(code, status_class(code))GATE problem patterns
First identify whether a question asks about the operation, the result, or metadata. Do not infer success from the method: a GET can produce a 404, and a POST can produce a 201. For a 304 Not Modified response, the client normally reuses a cached representation rather than receiving the complete body again.
Exercise
A client sends PUT /item/4 twice, and both requests replace item 4 with the same representation. Which property does this illustrate?
Show answer
Idempotence. Repeating the operation has the same intended final effect as performing it once.