Sub topic 6
Methods: GET, POST, and friends
This chapter assumes you can read a basic HTTP request and response, as covered in the previous chapter, and that you can run commands in a terminal. It covers what an HTTP method is, how the common methods differ, and the errors people make with them.
What a method tells the server
The method is the first word of the request line. It states what the client wants done with the resource named by the path. The server decides what actually happens, but well-behaved servers follow the meaning of each method.
POST /orders HTTP/1.1
Host: example.com
Content-Type: application/json
Content-Length: 21
{"item":"book","qty":2}The common methods
| Method | Typical use | Body in request | Safe | Idempotent |
|---|---|---|---|---|
| GET | Read a resource | No | Yes | Yes |
| HEAD | Read headers only | No | Yes | Yes |
| POST | Create or trigger processing | Yes | No | No |
| PUT | Replace a resource | Yes | No | Yes |
| PATCH | Change part of a resource | Yes | No | Not guaranteed |
| DELETE | Remove a resource | Usually no | No | Yes |
| OPTIONS | Ask what is allowed | No | Yes | Yes |
Two terms matter here:
- A safe method does not change state on the server. Clients, caches, and crawlers can call it freely.
- An idempotent method has the same effect whether you send it once or several times. Deleting the same item twice leaves the same end state.
Idempotence decides whether a client may retry automatically after a network failure. Retrying a PUT is fine. Retrying a POST can create a duplicate order.
GET versus POST
GET puts its inputs in the URL query string. The URL appears in browser history, server logs, and the Referer header, so never put passwords or tokens in it. Browsers and caches may store GET responses.
POST puts its inputs in the body. The body is not part of the URL, and responses are rarely cached. Use POST when the request changes something or carries sensitive or large data.
curl -v "https://example.com/search?q=http"
curl -v -X POST https://example.com/orders \
-H "Content-Type: application/json" \
-d '{"item":"book","qty":2}'With -d, curl already sends POST, so -X POST is optional there. Use -X for other methods such as -X DELETE.
Handling methods on a server
A server routes on both method and path. The same path can do different things depending on the method.
import { createServer } from "node:http";
const server = createServer((req, res) => {
if (req.url === "/items" && req.method === "GET") {
res.writeHead(200, { "Content-Type": "application/json" });
res.end(JSON.stringify([{ id: 1 }]));
} else if (req.url === "/items") {
res.writeHead(405, { Allow: "GET" });
res.end();
} else {
res.writeHead(404);
res.end();
}
});
server.listen(3000);The 405 Method Not Allowed response includes an Allow header listing the methods the resource accepts.
Common errors
- Changing state with GET. A link such as
/delete?id=5can be triggered by prefetching, crawlers, or a malicious page. Use POST or DELETE. - Retrying POST blindly. Reloading a page after a form POST makes the browser ask before resubmitting. A common fix is to answer the POST with a 303 redirect to a GET page.
- Assuming a method is enforced. The method is a convention. Servers must check it themselves.
Exercise
A client sends a PUT to /users/7 and the connection drops before a response arrives. Is it safe to send the same request again? What about a POST to /orders?
Show answer
Yes for the PUT, because PUT is idempotent: repeating it leaves the user in the same state. A POST to /orders is not idempotent, so a retry may create a second order. The client should check first, or use an idempotency key if the server supports one.