Menu
HTTP methods
“GET, POST, PUT, PATCH, DELETE: the verbs of HTTP.”
The first word of every request is the method. It says what you want to do with the resource in the path.There are a few more, like HEAD (only the headers, no body) and OPTIONS (“what am I allowed to do here?”), which browsers send for cross-site requests.
DELETE /users/42 HTTP/1.1
Host: api.example.comThe common ones#
| Method | Meaning | Has a body? | Safe | Idempotent |
|---|---|---|---|---|
| GET | Read a resource | No | ✅ | ✅ |
| POST | Create something / do an action | Yes | ❌ | ❌ |
| PUT | Replace a resource completely | Yes | ❌ | ✅ |
| PATCH | Change part of a resource | Yes | ❌ | ❌ |
| DELETE | Remove a resource | Usually not | ❌ | ✅ |
- Safe means it doesn’t change anything on the server. Browsers and crawlers may send safe requests freely, so never let a
GETdelete data. - Idempotent means sending it twice has the same effect as sending it once. Deleting user 42 twice still leaves user 42 deleted. That makes it safe to retry after a network error.
Important
A POST is not idempotent: retrying “place order” can create two orders. That’s why browsers ask “Confirm form resubmission?” when you reload after submitting a form.
A small REST example#
APIs often combine methods with paths like this (a style called REST):
GET /todos → list all todos
POST /todos → create a todo
GET /todos/7 → read todo 7
PATCH /todos/7 → update part of todo 7
DELETE /todos/7 → delete todo 7Try them on a pretend API#
Pick a method, press Send, and read the raw request and response. Try POST twice, DELETE twice, and a PATCH without logging in.
Trying them with curl#
curl sends GET by default. Use -X to pick another method:
curl https://jsonplaceholder.typicode.com/todos/1
curl -X DELETE https://jsonplaceholder.typicode.com/todos/1And the same from JavaScript with fetch:
1const res = await fetch("https://jsonplaceholder.typicode.com/todos/1", {
2 method: "PATCH",
3 headers: { "Content-Type": "application/json" },
4 body: JSON.stringify({ completed: true }),
5});
6
7console.log(res.status); // 200
8console.log(await res.json()); // the updated todo
Note
JSONPlaceholder is a free fake API for practice. It answers like a real one but doesn’t actually save your changes.
Answer these 3 questions to commit this byte to your learning path.
Reset. This byte isn't committed any more. Answer the questions again to commit it.
$ git commit -m "learn(http): HTTP methods"
[main 610f108] learn(http): HTTP methods
1 byte changed, 3 insertions(+)
See it on your path Already committed . Try the questions again any time, it won't undo anything. To uncommit it and start over: