A Bookshop admin clicks Save on the new-book form. The screen sits still for a moment, so they click once more. Those two clicks send the exact same request, POST /books, twice, and the server makes two identical books. If the Delete button were clicked twice instead, no data would be doubled. The difference is in the first word of each request.
That first word is called the HTTP method: the verb that says what to do with the resource at the address after it. The address is the noun (/books/2), and the method is the verb (GET, DELETE). Think of a shelf in a bookshop. At the same shelf you can look at what's on it, put a new book there, swap one book for a new edition, fix just one book's price label, or remove a book from the shelf. The shelf stays the same, and only what you do changes. In an API, the shelf is the address, and each of those actions is one method.
Five methods in the Bookshop API
| Method | Meaning | Example | Answer when it works |
|---|---|---|---|
GET |
fetch | GET /books/2 |
200 OK + the book |
POST |
create new | POST /books |
201 Created + header Location: /books/4 |
PUT |
replace entirely | PUT /books/2 |
200 OK + the new version |
PATCH |
change part | PATCH /books/2 |
200 OK + the new version |
DELETE |
delete | DELETE /books/2 |
204 No Content, no body |
GETfetches data,POSTadds new data,DELETEdeletes.POSTis sent to the collection (the address of all books,/books), because the server picks the new book's number. The answer is201 Created, and theLocationheader gives the new book's address.PUT,PATCHandDELETEare sent to one book (/books/2), because you have to say which book.- A successful
DELETEis usually answered with204 No Content(success, no body). Some servers send a body as a confirmation, such as the data of the book just deleted. In that case, the status is200 OK.
A method that doesn't fit its address is answered with 405 Method Not Allowed, together with an Allow header. For example, PUT /books (no book number) is answered with Allow: GET, POST: at /books, only those two methods are allowed. But not every server sends 405 by itself. Express, for example, answers 404. The lesson Your First Endpoint shows how to add 405.
PUT replaces, PATCH patches
This is book number 2 on the server, before the change:
{"id": 2, "title": "This Earth of Mankind", "author_id": 2, "price": 125000, "stock": 0}
PATCH /books/2 with the body {"stock": 7} only changes the field (one piece of data) that was sent, here stock. The result:
{"id": 2, "title": "This Earth of Mankind", "author_id": 2, "price": 125000, "stock": 7}
PUT /books/2 with the body {"title": "This Earth of Mankind", "price": 99000} replaces everything in book 2 with that body. The result:
{"id": 2, "title": "This Earth of Mankind", "price": 99000}
author_id and stock are gone, because they weren't sent. A stricter API would refuse an incomplete PUT. So the rule is: with PUT, send every field. With PATCH, send only what changed.
Safe and idempotent
Two properties of a method decide when a request may be sent again:
- Safe: it doesn't change data on the server.
GETis safe. - Idempotent: sent once or ten times, the request leaves the same final data on the server.
GET,PUTandDELETEare idempotent.POSTis not.PATCHisn't guaranteed to be.
| Request | First send | Second send | Final data |
|---|---|---|---|
POST /books |
201, book 4 created |
201, book 5 created |
two new books with the same content |
PUT /books/2 |
200 |
200 |
the same: price 99000 |
DELETE /books/2 |
204 |
404 |
the same: book 2 is gone |
So the Save button clicked twice makes two new books, numbers 4 and 5. Look at the DELETE row: its second answer is different (404, because book 2 is already gone), but the data on the server is the same. So DELETE is still idempotent. Idempotence is about the data on the server, not about the answer.
Idempotence matters when the network connection breaks in the middle of a request. The client doesn't know whether its request arrived, so it sends it again. PUT and DELETE can be sent again, because the final data stays the same. A POST sent again can create duplicate data.
GET is only for reading
Say an API changes student 5's name to Budi Santoso with the request GET /api/students/5?name=Budi+Santoso. The part after ? carries the new name (the next lesson covers this part). It looks convenient, but it's wrong: GET must not change data. Browsers, search engines and caches (stores of copies of answers) may send a GET at any time, without anyone meaning to change anything. For example, a browser can load a page early, before you open it. To change the name, send PATCH /api/students/5 with the body {"name": "Budi Santoso"}.
Exercise: four endpoints for books
In the exercise, app has one function for each method. The shape of the code is always the same:
app.get("/books", (req, res) => {
res.json({ method: "GET" });
});
app.post("/books", (req, res) => {
res.status(201).json({ method: "POST" });
});
app.get,app.post,app.put,app.deleteregister an endpoint for the method in their name.res.status(201)sets the status first, then.json(...)sends the body. Withoutres.status, the status is200.- In the path
"/books/:id", the part that starts with a colon (:id) can hold anything, such as1in/books/1. Its value is read fromreq.params.id. The next lesson covers this. - In this exercise,
DELETEanswers204with no body:res.status(204).send().
⚠️ The wrong method, the wrong status
- Using POST for everything.
POST /books/2/deletehides the action in the URL. WriteDELETE /books/2. - A successful POST answered with 200. A
POSTthat creates data is answered with201 Created, ideally with aLocationheader. - A PUT with half a body. If you only want to change the stock, use
PATCH, so the other fields don't disappear. - A method that doesn't fit its address.
PUT /booksorPOST /books/2is answered with405 Method Not Allowed.POSTgoes to the collection (/books), whilePUT,PATCHandDELETEgo to one book (/books/2).
TL;DR: The method is the verb of a request. GET fetches data (200). POST creates new data in the collection (/books, the address of all books) and is answered with 201 Created, plus a Location header that holds the new data's address. PUT replaces the whole thing, PATCH changes part of it, and DELETE deletes (204, or 200 with a body). GET is safe: it doesn't change data, so never use GET to change data. GET, PUT and DELETE are idempotent: repeat them any number of times and the final data on the server is the same. POST is not: if the Save button is clicked twice, POST /books is sent twice and the server makes two books. A method that doesn't fit its address is answered with 405, plus an Allow header (the list of methods allowed at that address).
⚡ The method is the verb
Meaning: The method is the first word of a request. The address is the noun (/books/2), and the method is the verb: GET fetches, POST creates new data, PUT replaces the whole thing, PATCH changes part of it, DELETE deletes.
⚡ POST creates new data
Write:
app.post("/books", (req, res) => {
res.status(201).json({ method: "POST" });
});
Guess first: the Save button on the new-book form is clicked twice, so POST /books is sent twice. How many new books?
Two, numbers 4 and 5. Every POST sent creates new data, so POST isn't idempotent. PUT and DELETE are idempotent: repeat them any number of times and the final data on the server is the same.
Meaning: app.post registers an endpoint for the request POST /books. POST is sent to the collection (/books, the address of all books) and answered with 201 Created. res is what you answer with: res.status(201) sets the status first, then .json(...) sends the body (the content of the answer).
⚡ PUT replaces, PATCH patches
Meaning: Book number 2 has the fields (pieces of data) title, author_id, price and stock. PUT /books/2 with the body {"title": "This Earth of Mankind", "price": 99000} replaces everything in book 2 with that body, so author_id and stock are gone. That's why you send every field with PUT. With PATCH, send only what changed, such as {"stock": 7}, and the other fields stay.
⚡ GET is only for reading
Meaning: GET must not change data, because browsers, search engines and caches (stores of copies of answers) may send a GET at any time. So GET /api/students/5?name=Budi+Santoso, which changes student 5's name, is wrong. To change the name, send PATCH /api/students/5 with the body {"name": "Budi Santoso"}.