HTTP Methods: GET, POST, PUT, PATCH and DELETE

2.HTTP Methods: GET, POST, PUT, PATCH and DELETE

M

In this chapter

We'll learn what the HTTP methods GET, POST, PUT, PATCH and DELETE (plus HEAD and OPTIONS) mean, what "safe" and "idempotent" promise and why browsers repeat some requests freely, which method fits which action, how PHP tells methods apart, why HTML forms only send GET and POST, and how to answer a wrong method with 405.

14–16 min

The Problem in Real Life

Looking for other surprises, Marcus finds the barbers' "Cancel" button: a plain link, <a href="/cancel.php?id=4811">. It works — and so does every browser that prefetches links to make pages faster, every link-preview bot in a chat app, and every search engine that follows links.

"A link is a GET," Marcus says. "And a GET must never change anything. Somebody's browser is going to cancel a haircut just by looking at this page."

M

The method is a promise. GET says 'just looking'. Our cancel link lies.

Marcus

Any Method for Anything vs. The Method That Matches the Action

Safe methods

GET and HEAD only read. Browsers, bots and caches may send them at any time.

Idempotent methods

PUT and DELETE give the same result if repeated. POST doesn't.

Wrong method, real damage

A GET that cancels or a POST behind a refresh changes data nobody meant to change.

HTTP Methods: GET, POST, PUT, PATCH and DELETE

What the request wants to do: the first word of every request is its method — the verb. Choosing the right one isn't style; browsers, caches and other programs behave differently for each. This chapter covers:

  • GET and HEAD: reading.
  • POST: creating and other actions.
  • PUT, PATCH and DELETE: replacing, changing and removing.
  • Safe and idempotent: the two promises.
  • Methods in PHP: REQUEST_METHOD, forms, method spoofing and 405.

GET and HEAD: GET reads a resource — a page, the free slots, an image. Its data travels in the URL's query string (?date=2026-11-07), not in a body, so a GET can be bookmarked, shared, cached and reloaded freely. HEAD is a GET without the body: only the status and headers, used to check whether something exists or has changed. Neither may change anything on the server — that's the rule the cancel link breaks.

The bug: a link that changes data
<!-- the barbers' schedule page -->
<a href="/cancel.php?id=4811">Cancel</a>
<!-- the fix: a POST form, so only a deliberate click can cancel -->
<form method="post" action="/cancel.php">
<input type="hidden" name="id" value="4811">
<button>Cancel</button>
</form>

POST: POST sends data in the body for the server to process: create a booking, send a message, log in. Each POST may create something new, so sending it twice can create two things — Sam's double booking. That's why browsers ask before resubmitting a POST, and never prefetch or cache one.

PUT, PATCH and DELETE: these three are mostly used by APIs (chapter 9). PUT replaces a resource with the one in the body — "booking 4811 is now exactly this". PATCH changes part of it — "booking 4811's time is now 16:30". DELETE removes it. The URL names which resource (/api/bookings/4811); the method says what to do with it. Two more: OPTIONS asks which methods a URL accepts (CORS uses it, chapter 7), and HEAD, above.

Safe and idempotent: two words sum up the rules. A safe method doesn't change anything: GET, HEAD, OPTIONS. Anyone — a browser prefetching links, a crawler, a cache — may send it at any time. An idempotent method has the same effect whether it's sent once or five times: PUT (the booking ends up the same), DELETE (gone is gone) and all safe methods. POST and PATCH are neither: repeating them can repeat the effect. This is why networks and clients may retry a timed-out GET or PUT automatically, but never a POST. The table below sums it up, and the diagram shows how a browser treats each.

Table — The methods at a glance
MethodMeaningBody?Safe?Idempotent?BookBay example
GETReadNoYesYesGET /api/slots?date=2026-11-07
HEADRead headers onlyNoYesYesDoes booking 4811 exist?
POSTProcess / createYesNoNoPOST /book.php — a new booking
PUTReplaceYesNoYesPUT /api/bookings/4811 with the full booking
PATCHChange partYesNoNo (in general)PATCH /api/bookings/4811 {"time": "16:30"}
DELETERemoveRarelyNoYesDELETE /api/bookings/4811
OPTIONSWhich methods are allowed?NoYesYesCORS preflight (chapter 7)

How a browser treats each method

GET /booked.php?id=4811

safe

POST /book.php

not safe, not idempotent

browser

Refresh: sent again silently

prefetch and cache allowed

Refresh: "Confirm Form Resubmission"

never prefetched or cached

so

Repeating it is harmless

if the page only reads

Repeating it repeats the effect

a second booking

Methods in PHP: $_SERVER['REQUEST_METHOD'] holds the method as a string, and a single script often handles both: show the form on GET, process it on POST. HTML forms only send GET or POST; frameworks like Laravel fake the others with a hidden field _method=DELETE that the server reads instead — called method spoofing. When a script receives a method it doesn't support, answer 405 Method Not Allowed with an Allow header listing the methods it does accept. The fix for the cancel link: a small form with method="post" and a button.

One script, two methods — and 405 for the rest
<?php
// public/cancel.php
$method = $_SERVER['REQUEST_METHOD'];
if ($method === 'GET') {
echo "<p>Cancel booking #", (int) $_GET['id'], "?</p>"; // only shows a question
} elseif ($method === 'POST') {
cancelBooking((int) $_POST['id']); // the only place that changes data
header('Location: /schedule.php', true, 303);
} else {
http_response_code(405);
header('Allow: GET, POST');
}
Real output: a method the script doesn't support
$ curl -i -X PUT http://localhost:8001/book.php
HTTP/1.1 405 Method Not Allowed
Allow: GET, POST
Content-type: text/html; charset=UTF-8

Key Takeaway

The method says what a request wants: GET reads (data in the query string, bookmarkable, cacheable), HEAD is GET without a body, POST asks the server to process a body (often creating something), PUT replaces a resource, PATCH changes part of one, DELETE removes it, OPTIONS asks what's allowed. Safe methods (GET, HEAD, OPTIONS) must never change data, because browsers, bots and caches send them freely; idempotent ones (also PUT and DELETE) can be repeated safely, while POST and PATCH can't — which is why browsers warn before resubmitting a POST. In PHP, branch on $_SERVER['REQUEST_METHOD'], answer unsupported methods with 405 and an Allow header, and never change data from a link.

Why This Matters

Using GET for actions is a classic bug with real consequences: crawlers deleting content, prefetching cancelling orders, link previews triggering actions. The method rules prevent all of it for free.

The same methods structure every REST API (chapter 9) and every Laravel route file: Route::get(), Route::post(), Route::delete(). Knowing what each promises tells you which one to pick.

The cancel link is now a form that POSTs, and unsupported methods get a 405. Next, Mia needs to read what each request actually carries: the date in ?date=2026-11-07, the booking id in /bookings/4811, the fields of a form, and the JSON a mobile app will send. Each one arrives in a different place. That's the next chapter: parameters, request body, form data and JSON.