Sub topic 14
Methods, status codes, and common headers
This chapter assumes you can read a basic HTTP request and response (request line, headers, blank line, body) and know that HTTP runs over a TCP byte stream. It covers what the method, the status code, and the main headers tell client and server, and the mistakes GATE questions probe.
Methods: what the client asks for
The method is the first word of the request line. It names the action the client wants on the resource. Two properties of methods matter most in exam questions.
- A method is safe if it is not meant to change server state. Safe methods are read-only by convention.
- A method is idempotent if sending the same request many times has the same effect on the server as sending it once.
| Method | Purpose | Safe | Idempotent | Request body |
|---|---|---|---|---|
| GET | Retrieve a representation of the resource | Yes | Yes | Normally none |
| HEAD | Same as GET but the response has headers only | Yes | Yes | None |
| POST | Submit data for processing, such as creating a record | No | No | Yes |
| PUT | Replace the resource at the URL with the body | No | Yes | Yes |
| DELETE | Remove the resource | No | Yes | Usually none |
| OPTIONS | Ask which methods the server supports | Yes | Yes | None |
Two points often confuse learners.
- PUT is idempotent and POST is not. Sending the same PUT twice leaves the resource in the same state. Sending the same POST twice may create two orders.
- HEAD returns the same headers as GET would, including Content-Length, but no body. It is used to check size, type, or freshness without downloading the content.
"Safe" is a convention, not enforcement. A badly written server can change state on a GET. Web crawlers and prefetching assume GET is safe, which is why state-changing actions should not use it.
Status codes: how the server answers
The status line of a response holds a three-digit code and a reason phrase, for example HTTP/1.1 404 Not Found. The first digit gives the class.
| Class | Meaning | Common codes |
|---|---|---|
| 1xx | Informational, request received and processing continues | 100 Continue, 101 Switching Protocols |
| 2xx | Success | 200 OK, 201 Created, 204 No Content |
| 3xx | Redirection, the client must take further action | 301 Moved Permanently, 302 Found, 304 Not Modified |
| 4xx | Client error, the request is wrong | 400 Bad Request, 401 Unauthorized, 403 Forbidden, 404 Not Found |
| 5xx | Server error, a valid request failed | 500 Internal Server Error, 502 Bad Gateway, 503 Service Unavailable |
Distinctions worth memorizing:
- 401 vs 403. 401 means the client has not authenticated (or the credentials failed). 403 means the server understood who you are and refuses anyway.
- 301 vs 302. 301 says the move is permanent, so clients and caches may remember the new URL. 302 is temporary, so the original URL stays in use. Both carry the new address in a
Locationheader. - 304 Not Modified has no body. It answers a conditional request and tells the client to reuse its cached copy.
- 404 vs 410. 404 says the server has no such resource, with no claim about permanence. 410 says it is gone for good.
- 502 vs 503. 502 means a gateway or proxy got a bad answer from an upstream server. 503 means the server is temporarily unable to handle the request.
Common headers
Headers are Name: value lines. Some appear only in requests, some only in responses, and some in both.
| Header | Direction | Purpose |
|---|---|---|
| Host | Request | Names the target host. Required in HTTP/1.1, so one IP address can serve many sites. |
| User-Agent | Request | Identifies the client software. |
| Accept | Request | Media types the client can handle. |
| Content-Type | Both | Media type of the body, such as `text/html` or `application/json`. |
| Content-Length | Both | Body size in bytes. |
| Location | Response | Target URL for a redirect. |
| Server | Response | Identifies the server software. |
| Connection | Both | `close` or `keep-alive`, controlling the TCP connection. |
| Cookie / Set-Cookie | Request / Response | State handling, covered in the cookies chapter. |
| If-Modified-Since | Request | Makes the request conditional on the resource having changed. |
| Last-Modified, ETag | Response | Version markers used for caching. |
Content-Length matters at the TCP level. TCP delivers a byte stream with no message boundaries, so the receiver needs to know where the body ends. Content-Length provides the byte count. Without it, the server may signal the end by closing the connection, or use chunked transfer encoding.
A request and response in practice
The program below sends a HEAD request over a plain TCP socket and prints the status line and headers. It shows that a HEAD response has headers but no body.
import socket
host = "example.com"
request = (
f"HEAD / HTTP/1.1\r\n"
f"Host: {host}\r\n"
f"Connection: close\r\n"
f"\r\n"
)
with socket.create_connection((host, 80)) as s:
s.sendall(request.encode())
response = b""
while True:
chunk = s.recv(4096)
if not chunk:
break
response += chunk
head, _, body = response.partition(b"\r\n\r\n")
print(head.decode())
print("body bytes:", len(body))The output starts with a status line such as HTTP/1.1 200 OK, followed by headers such as Content-Type and Content-Length. The body length printed is 0, even though Content-Length may name a positive number. That header describes what a GET would return.
The diagram shows a conditional GET, which combines methods, headers, and status codes.
Common errors
- Mixing up safe and idempotent. DELETE is idempotent but not safe. GET is both. POST is neither.
- Treating 3xx as an error. Redirects are normal flow. The client follows
Locationwith a new request. - Forgetting the blank line. The header section ends with an empty line (
\r\n\r\n). Without it the server keeps waiting for more headers. - Omitting
Hostin HTTP/1.1. A compliant server rejects such a request with 400 Bad Request. - Assuming a 304 carries a body. It does not, so the client must use its stored copy.
- Reading the reason phrase as meaningful. Clients act on the numeric code. The phrase is only text for humans.
Exercises
Exercise
A client sends the same PUT request to a server three times in a row, and the first one creates the resource. After the third request, how does the server state differ from the state after the first? Would the answer change if the method were POST creating a new record each time?
Show answer
With PUT the state is identical after the first and third request, because PUT is idempotent: each request replaces the resource at that URL with the same body. With POST the state would differ, since each request can create a separate record, so three requests give three records.
Exercise
A browser requests a page it cached earlier, using If-Modified-Since. The page has not changed. Name the status code the server returns, state whether the response has a body, and say what the browser then displays.
Show answer
The server returns 304 Not Modified. The response has no body. The browser displays the copy from its own cache.
Exercise
A request for /old-page receives a response with status 301 and the header "Location: /new-page". Describe what the client does next and why 301 differs from 302 for caching.
Show answer
The client sends a new GET request for /new-page. Because 301 means the move is permanent, the client or a cache may remember the redirect and go straight to /new-page in future. A 302 is temporary, so the client should keep using /old-page for later requests.