15 min read
HTTP methods: GET, POST, PUT, PATCH and DELETE
In one sentence: The method is the first word of an HTTP request and says what you want to do with what you ask for: read it (GET), send it data (POST), replace it (PUT), change one part (PATCH) or delete it (DELETE). Each method makes a promise, and the server has to keep it.
What you will learn
- Choose the method that fits each action, and tell PUT from PATCH.
- Explain what a safe method and an idempotent one are, and what happens when a request is repeated.
- Use HEAD to see only the headers and OPTIONS to ask which methods a resource accepts.
- Read a 405 and its Allow header, and take it away from the shop's nginx and give it back.
Before you start, read: 01-request-and-response.md phase-1/06-services-and-packages.md
The problem
Section titled “The problem”Imagine Luis is paying for an order at the shop from the train. He taps “Pay”, the screen keeps loading and nothing happens. He taps it again. Does he get charged twice? It depends on how his phone talks to the server, and on what the server has promised.
Another case, a real one. A large government website suddenly lost its content. It wasn’t an attack: Google’s robot crawled it by following links, as it always does, and among them were links to delete pages. It followed them all. They were meant for the editors, but the site only protected them with a and with JavaScript, and the robot uses neither. The Daily WTF told the story in 2006.
Both have the same root: a request that gets repeated, or that someone other than you sends. HTTP solves it with the first word of every request, the method. In Phase 0 you only wrote GET; now you’re on the side that answers, and you decide which methods you accept and what you reply to the rest.
The analogy
Section titled “The analogy”The analogy
Think of a bank counter. You can ask for several things, and each one is requested with its own form:
- Checking your balance. You can ask ten times: the account doesn’t change.
- Making a deposit. Each form you hand in is another deposit. If you hand it in twice, you deposit twice.
- Changing your address to the one on the form. If you hand it in twice, the address ends up the same.
- Closing the account. The second time, the clerk tells you “it’s already closed.” The answer changes, but the account ends up the same: closed.
And if you try to make a deposit at the inquiries counter, the clerk doesn’t accept it, but tells you what you can do there: “only inquiries here.” That’s a 405 with its Allow HeaderInformation a protocol puts in front of its data to do its job. In HTTP they are text lines of the form “Name: value”, such as Host or Content-Type; in TCP or IP they are numbers in fixed positions, such as ports or addresses.Go to definition.
Where the analogy breaks down
- The clerk looks at what you do. In HTTP, the method is a promise that whoever programmed the server keeps, or doesn’t: nothing stops you from writing a server that deletes with a
GET. That’s what happened to the government website. - A clerk understands what you want even if you bring the wrong form. A server doesn’t guess what you meant: if the method is no good to it, it answers with an error.
- Some changes can be repeated (“my phone number is this one”) and others can’t (“add €10 to it”). The same happens with
PATCH, which you’ll see later.
How it really works
Section titled “How it really works”The method is the first word of the request
Section titled “The method is the first word of the request”These two requests go to the same address, but they ask for different things:
GET /products HTTP/1.1Host: localhost:8080POST /products HTTP/1.1Host: localhost:8080Content-Type: application/jsonContent-Length: 25
{"name":"Cap","price":15}The first asks for the list of products. The second asks to create a new product, the cap, with the data that goes in the Body (of a request or response)The data an HTTP message carries after the blank line that ends the headers, such as the fields of a form, some JSON or an image. The Content-Type header says what format it’s in, and Content-Length how many bytes it takes up.Go to definition, the one you saw in lesson 1, “Request and response”. The difference is set by the first word, the HTTP methodHTTP stands for HyperText Transfer Protocol.The word every HTTP request starts with, which says what the client wants to do with what it asks for. GET is reading, POST sending data, PUT replacing, PATCH changing a part and DELETE deleting.Go to definition.
The path says what you act on, the resource, and the method says what you do with it. This is what the shop’s full API could look like (today, its nginx only answers GET /products):
| Request | What it asks for |
|---|---|
GET /products |
The list of products |
POST /products |
Create a product with the data in the body; the server gives it a number |
GET /products/2 |
One product: the mug |
PUT /products/2 |
Replace the whole mug with what goes in the body |
PATCH /products/2 |
Change only one part of the mug, like the price |
DELETE /products/2 |
Delete the mug |
What each method promises
Section titled “What each method promises”Each method has a meaning fixed in the HTTP standard, RFCRFC stands for Request for Comments.The public documents where the exact rules of internet protocols are written. HTTP/1.1, for example, is defined in RFCs 9110 and 9112.Go to definition 9110, except PATCH, which came later with its own RFC, 5789. These are the ones you’ll use:
| Method | What for | Safe? | Idempotent? | Usually has a body? |
|---|---|---|---|---|
GET |
Read a resource | Yes | Yes | No |
HEAD |
The same as GET, but only the headers |
Yes | Yes | No |
OPTIONS |
Ask what can be done with a resource | Yes | Yes | No |
POST |
Send data for the server to do something: create, pay, send a message | No | No | Yes |
PUT |
Replace a whole resource, or create it at a path you choose | No | Yes | Yes |
PATCH |
Change one part of a resource | No | Not always | Yes |
DELETE |
Delete a resource | No | Yes | No |
RFC 9110 defines two more, CONNECT and TRACE, which you won’t use, and only requires accepting GET and HEAD. The “Safe?” and “Idempotent?” columns are its promises, and each one deserves a section.
Safe: it only reads
Section titled “Safe: it only reads”Ask for GET /products a hundred times: the list stays the same. That’s a Safe methodAn HTTP method that only asks to read, without changing anything on the server, such as GET or HEAD. That’s why browsers and search engines use it without asking you. It has nothing to do with security or encryption.Go to definition: one that doesn’t ask for any change on the server.
Something does happen: each GET adds a line to the server’s LogA text file where a program writes down what happens, one line for each thing. A web server’s has a line for each request it receives, and it’s the first thing you look at when something fails.Go to definition. But it isn’t a change the client asked for.
Why it matters: many programs make GET requests without asking you.
- Search engine robots follow every link they find.
- Messaging apps request the page when you paste a link, to show the preview.
- Some email filters open the links in messages to check whether they’re dangerous.
- The browser may request pages in advance when it thinks you’re going to visit them.
They all trust that GET doesn’t change anything. A link is always a GET, so a “Delete” link breaks that trust: it’s the story of the government website.
Idempotent: repeating it doesn’t change the result
Section titled “Idempotent: repeating it doesn’t change the result”Now delete the mug twice. The first DELETE /products/2 deletes it, and the server answers that everything went fine. The second finds nothing to delete, and answers 404, “that doesn’t exist.” The answers are different, but the shop ended up the same both times: without a mug.
A request is IdempotentSaid of an operation that leaves things in the same state whether you do it once or ten times. In HTTP, GET, PUT and DELETE are idempotent and POST isn’t, so repeating a POST can create two orders.Go to definition if making it once or ten times leaves the server the same. Notice that what’s measured is the effect, not the answer. PUT is too: replacing the mug with the same data twice leaves it the same. And every safe method is, because they don’t change anything.
POST isn’t: each POST /orders creates a new order. And that’s what can happen to Luis on the train:
- The shop's servercreates order 41 and charges
- The shop's server → Luis's phonethe response gets lost on the way
- Luis's phonenothing arrives: should I try again?
- The shop's servercreates order 42 and charges again
Why it matters: networks fail. If the response doesn’t arrive, the client doesn’t know whether its request arrived or not. With an idempotent method it can repeat it without fear; with one that isn’t, repeating it can do the same thing twice. That’s why the standard asks programs not to automatically repeat a request that isn’t idempotent.
If a POST has to be repeatable, like a payment, the client sends a unique key with it; if the server sees the same key twice, it returns the first result without charging again. That’s how payment APIs like Stripe’s do it.
PUT replaces, PATCH changes a part
Section titled “PUT replaces, PATCH changes a part”Now the mug looks like this: {"id":2,"name":"Mug","price":8.5}. Ana wants to raise its price to €9. There are three ways to ask for it, and they don’t give the same result:
| Request | Body | How the mug ends up |
|---|---|---|
PUT /products/2 |
{"name":"Mug","price":9} |
{"id":2,"name":"Mug","price":9} |
PUT /products/2 |
{"price":9} |
{"id":2,"price":9}: it has lost its name |
PATCH /products/2 |
{"price":9} |
{"id":2,"name":"Mug","price":9} |
PUT replaces the whole resource with what you send. If you only send the price, the rest disappears. PATCH only changes what you send and leaves the rest as it was.
And is PATCH idempotent? “The price is 9” is: repeating it leaves 9. But “add one unit to the stock” isn’t, and it can also be requested with PATCH. That’s why the standard doesn’t promise it.
HEAD and OPTIONS: asking without requesting the content
Section titled “HEAD and OPTIONS: asking without requesting the content”HEAD is a GET without content: the server answers with the same headers, but without the body. It’s for finding out whether something exists, how big it is (Content-Length) or what type it is, without downloading it. curl -I sends a HEAD.
OPTIONS asks: “what can I do with this?” The response should carry the Allow header, with the list of methods that resource accepts. A person almost never sends it: the browser sends it on its own, before certain requests to other websites. You’ll see it in lesson 6, “CORS”.
The 405 and the Allow header
Section titled “The 405 and the Allow header”If a resource doesn’t support a method, the server answers 405 Method Not Allowed. It isn’t a 404: the resource exists.
The standard is clear: a 405 must carry the Allow header, with the methods it does accept. That way, whoever gets the error knows what they can do. But not every server complies:
| Server | Response to a POST |
Has Allow? |
|---|---|---|
| backenddesdecero.com (Cloudflare) | 405 |
No |
| nginx serving files, like the welcome page on your VM’s port 80 | 405 Not Allowed |
No |
nginx with the shop’s configuration, on /products |
405 Not Allowed |
Yes: Allow: GET, HEAD |
The shop has it because its configuration adds it by hand, with add_header Allow. (Not Allowed is nginx’s phrase; what counts is the number.)
Try it
Section titled “Try it”The goal: find out which methods each server accepts and which of the two follows the Allow rule. Then you’ll make the shop’s nginx break it, and you’ll fix it.
On your computer: the course’s site
Section titled “On your computer: the course’s site”These three commands run on your computer. The outputs are from a Mac; on Linux and WSL they come out the same.
1. Ask only for the headers
curl -sI https://backenddesdecero.com/en/What you will see:
HTTP/2 200date: Mon, 23 Nov 2026 15:28:37 GMTcontent-type: text/htmlcf-cache-status: HITcache-control: public, max-age=0, must-revalidatestrict-transport-security: max-age=86400referrer-policy: strict-origin-when-cross-originx-content-type-options: nosniff…server: cloudflarecf-ray: a5f13468cf1a0061-MADalt-svc: h3=":443"; ma=86400-I sends a HEAD, and -s removes curl’s progress messages. The home page’s headers arrive, but not a single line of HTML: the 200 says the page exists, without downloading it.
They come out in lowercase because curl speaks HTTP/2 with this site (you’ll see it in lesson 8). We’ve removed two long Cloudflare headers (…); your dates and your cf-ray will be different.
2. Try to send it something
curl -si -X POST https://backenddesdecero.com/en/What you will see:
HTTP/2 405date: Mon, 23 Nov 2026 15:28:37 GMTcontent-length: 0strict-transport-security: max-age=86400referrer-policy: strict-origin-when-cross-originx-content-type-options: nosniff…server: cloudflarecf-ray: a5f13469cadaddef-MADalt-svc: h3=":443"; ma=86400-X POST changes the method, and -i shows the response headers before the content. Notice two things:
- The
405: the site is static, it only serves files, and it has nothing to do with what you send it. - What’s missing: no
allowheader. This site doesn’t follow the rule, and whoever gets the error doesn’t know what’s accepted.
If you get a 200, you forgot the -X POST.
Now, every method at once, with a for loop like the ones in lesson 8 of Phase 1:
3. Try every method
for m in GET POST PUT PATCH DELETE OPTIONS; docurl -s -o /dev/null -w "$m %{http_code}\n" -X $m https://backenddesdecero.com/en/doneWhat you will see:
GET 200POST 405PUT 405PATCH 405DELETE 405OPTIONS 405-o /dev/null throws away the content, and -w writes, when it finishes, the method and the Status codeA three-digit number at the start of every HTTP response that sums up what happened. 200 means “all good,” 404 “that doesn’t exist” and 400 “your request is malformed.”Go to definition. Paste the three lines at once.
Only GET works. Even OPTIONS, the method for asking what’s accepted, gets a 405 that says nothing. And Cloudflare doesn’t even check whether the file exists: POST /does-not-exist also gets a 405, not a 404.
On your VM: the shop’s server
Section titled “On your VM: the shop’s server”Log in to the VM with multipass shell course. You need the shop’s configuration, which you install in the introduction. If you don’t have the VM, read the outputs: they’re from an Ubuntu 24.04 with nginx 1.24.0.
4. The product list
curl -i localhost:8080/productsWhat you will see:
HTTP/1.1 200 OKServer: nginx/1.24.0 (Ubuntu)Date: Tue, 13 Oct 2026 07:59:54 GMTContent-Type: application/jsonContent-Length: 109Connection: keep-alive
[{"id":1,"name":"T-shirt","price":20},{"id":2,"name":"Mug","price":8.5},{"id":3,"name":"Hoodie","price":35}]A GET: the three products, in JSONJSON stands for JavaScript Object Notation.A format for writing data as text, with braces, quotes and colons: {"product":"mug","quantity":2}. People can read it, and so can almost any programming language, and it’s the most common format for the data that travels between a frontend and a backend.Go to definition, with Content-Length: 109. Here curl speaks HTTP/1.1, and the headers are capitalized.
If you get Couldn't connect to server, nginx isn’t listening on port 8080: check that you installed the shop’s configuration and that systemctl is-active nginx says active.
5. The same, with HEAD
curl -I localhost:8080/productsWhat you will see:
HTTP/1.1 200 OKServer: nginx/1.24.0 (Ubuntu)Date: Tue, 13 Oct 2026 07:59:54 GMTContent-Type: application/jsonContent-Length: 109Connection: keep-aliveThe same headers, Content-Length: 109 included, but without the list: it says how big it would be with a GET.
6. Try to create a product
curl -i -X POST localhost:8080/productsWhat you will see:
HTTP/1.1 405 Not AllowedServer: nginx/1.24.0 (Ubuntu)Date: Tue, 13 Oct 2026 07:59:54 GMTContent-Type: text/html; charset=utf-8Content-Length: 166Connection: keep-aliveAllow: GET, HEAD
<html><head><title>405 Not Allowed</title></head><body><center><h1>405 Not Allowed</h1></center><hr><center>nginx/1.24.0 (Ubuntu)</center></body></html>The same 405 as on backenddesdecero.com, but this time with Allow: GET, HEAD: this resource can only be read. The HTML is nginx’s error page, for browsers.
With -X PUT, -X DELETE or -X OPTIONS you get the same: this configuration doesn’t handle OPTIONS, which isn’t required, but its 405 already says what’s accepted.
Break the rule and fix it
Section titled “Break the rule and fix it”You’re going to take away the shop’s Allow header, to see that it’s your configuration that sets it and not nginx. Open the file:
7. Open the configuration
sudo nano /etc/nginx/sites-available/shopFind the line with Ctrl + W, type add_header Allow and press Enter. Put a # in front of it to turn it into a comment, a line nginx doesn’t read:
location = /products { if ($request_method !~ ^(GET|HEAD)$) { # add_header Allow "GET, HEAD" always; return 405; }Save with Ctrl + O and Enter, and quit with Ctrl + X. Then check and apply the change:
8. Check and reload
sudo nginx -t && sudo systemctl reload nginxWhat you will see:
nginx: the configuration file /etc/nginx/nginx.conf syntax is oknginx: configuration file /etc/nginx/nginx.conf test is successfulnginx -t checks the configuration, and && only lets the reload through if the check went well. reload applies the change without cutting anyone off, and writes nothing if it works.
If you get test failed, open the file again and check that you only added the #.
9. The same POST, again
curl -i -X POST localhost:8080/productsWhat you will see:
HTTP/1.1 405 Not AllowedServer: nginx/1.24.0 (Ubuntu)Date: Tue, 13 Oct 2026 07:59:59 GMTContent-Type: text/html; charset=utf-8Content-Length: 166Connection: keep-alive
<html><head><title>405 Not Allowed</title></head><body><center><h1>405 Not Allowed</h1></center><hr><center>nginx/1.24.0 (Ubuntu)</center></body></html>No Allow: your shop now answers like backenddesdecero.com. If it still shows up, repeat the command: right after the reload, nginx takes a moment to stop answering with the previous configuration.
Now, leave it as it was: open the file again with sudo nano /etc/nginx/sites-available/shop, delete the #, save and repeat step 8. With step 6 you check that Allow: GET, HEAD is back.
Challenge: the shop’s home page answers 200 OK to curl -i -X DELETE localhost:8080/. Did it delete anything? Why does it answer like that?Show answer
It didn’t delete anything. This is the response:
HTTP/1.1 200 OKServer: nginx/1.24.0 (Ubuntu)Date: Tue, 13 Oct 2026 07:59:54 GMTContent-Type: text/plain; charset=utf-8Content-Length: 89Connection: keep-alive
Ana and Luis's shop. Try /products, /offers, /cart, /api/orders, /img/ and /maintenance.Look for location = / in the configuration: it does return 200 with the home page’s text and doesn’t look at the method, so it answers the same to all of them. /products, on the other hand, has the if that checks $request_method.
A program that sends that DELETE will believe it worked. The right thing would be a 405 with Allow: GET, HEAD, as on /products.
You have already seen it
Section titled “You have already seen it”- Reloading after submitting a form. The browser asks you whether you want to resubmit it: that page arrived with a
POST, and repeating it can duplicate what you did. - In DevTools (how to open them). In the Network tab, click a request: under Headers, Request Method says its method. In Chrome, right-clicking the column titles lets you add the Method column; Firefox already shows it.
And if you code:
fetch(url)makes aGETunless you tell it otherwise; for another method,fetch(url, { method: 'DELETE' }).- An HTML form only sends
GETandPOST. That’s why Rails or Laravel simulatePUTandDELETEwith a hidden field,_method. - TanStack Query (React Query) retries a failing query three times, but never a mutation: reading can be repeated, changing maybe not.
Common mistakes
Section titled “Common mistakes”- Changing things with
GET. A “Delete” link will be followed by any robot, preview or email filter. Whatever changes something goes withPOST,PUT,PATCHorDELETE. - Thinking idempotent means “same response.” The second
DELETEcan answer404, and it’s still idempotent: what doesn’t change is the server’s state. - A
PUTwith only the fields that change. The server replaces the whole resource and the others are lost. To change one part,PATCH. - Confusing “safe” with protected. A safe method is one that only reads; it has nothing to do with protecting your data.
curl -X HEAD.curlwarns you (Warning: Setting custom HTTP method to HEAD with -X/--request may not work the way you want. Consider using -I/--head instead.) and, with HTTP/1.1, as on the VM, it sits waiting for content that isn’t going to arrive. Usecurl -I.- A
405withoutAllow, or a200to everything. If a resource doesn’t support a method, answer405and say which ones it supports; a200, like the one in the challenge, makes people believe it worked.
Summary
Section titled “Summary”- The method says what you do with a resource:
GETreads,POSTsends data to create or do something,PUTreplaces,PATCHchanges a part andDELETEdeletes. - Safe means it only reads; idempotent, that repeating it leaves the server the same.
GET,HEAD,PUTandDELETEcan be repeated without fear;POSTcan’t. HEADbrings the headers without the content, andOPTIONSasks which methods a resource accepts.- A
405must carryAllowwith the accepted methods, even though not every server complies. - In Phase 3 you’ll write a server in code, and for each path and each method you’ll decide what it does and what it answers.
Did you get it?
Section titled “Did you get it?”To review:
Further reading
Section titled “Further reading”- HTTP request methods (MDN): one page per method, with examples.
- 405 Method Not Allowed (MDN): the code and its
Allowheader. - RFC 9110, section 9: Methods: where the methods are defined, what’s safe and what’s idempotent.
- RFC 5789: PATCH: the
PATCHmethod and why it isn’t promised to be idempotent. - Idempotent requests (Stripe): how a payments API lets you repeat a
POSTwithout charging twice.