Methods in HTTP
HTTP methods, sometimes called verbs, tell the server what action the client wants to perform on a resource. The same URL, such as /api/tasks/7, can mean "read this task", "update it" or "delete it" depending on the method. Choosing the right method makes an API predictable, lets browsers and caches behave correctly and is the foundation of RESTful design.
The main methods and what they mean
| Method | Purpose | Has a body? | Safe | Idempotent |
|---|---|---|---|---|
| GET | Read a resource or a list | No | Yes | Yes |
| POST | Create a resource or trigger an action | Yes | No | No |
| PUT | Replace a resource completely | Yes | No | Yes |
| PATCH | Change part of a resource | Yes | No | Not guaranteed |
| DELETE | Remove a resource | Usually no | No | Yes |
| HEAD | Like GET but returns only headers | No | Yes | Yes |
| OPTIONS | Ask which methods are allowed; used in CORS preflight | No | Yes | Yes |
What safe and idempotent really mean
A safe method does not change server state. Browsers, crawlers and prefetchers assume they can call safe methods freely.
An idempotent method has the same effect whether it runs once or many times. Deleting task 7 twice leaves it deleted. Replacing it with PUT twice leaves the same final data. POST is not idempotent: submitting an order form twice may create two orders. That is why browsers warn you before resending a POST when you refresh, and why clients can safely retry PUT and DELETE after a timeout but should be careful with POST.
A RESTful set of routes
For a collection of tasks, a typical API maps methods to actions like this:
GET /api/tasks -> list tasks
POST /api/tasks -> create a task (returns 201 Created)
GET /api/tasks/7 -> read task 7
PUT /api/tasks/7 -> replace task 7
PATCH /api/tasks/7 -> update some fields of task 7
DELETE /api/tasks/7 -> delete task 7 (often returns 204 No Content)
Calling them from the browser only changes the method and body in fetch:
await fetch('/api/tasks/7', {
method: 'PATCH',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ done: true }),
});
await fetch('/api/tasks/7', { method: 'DELETE' });
PUT versus PATCH
The difference confuses many beginners. PUT sends the full new version of the resource; any field you leave out may be cleared. PATCH sends only the fields that change. If a task has a title, description and done flag, a PUT with only { "done": true } could wipe the title and description, while a PATCH with the same body changes just the flag.
Common mistakes with methods
Using GET for actions that change data is the most damaging. A link like /tasks/7/delete can be followed by a crawler, a link previewer or a browser prefetch, deleting data without anyone clicking. Changes belong in POST, PUT, PATCH or DELETE.
Another frequent issue is the HTML form limit. Plain forms only support GET and POST. To send PUT or DELETE, use fetch from JavaScript, or a method-override convention on the server.
Also be aware that sending a JSON body with a custom header, or using PUT, PATCH or DELETE, across origins triggers a CORS preflight OPTIONS request. If the server does not answer that OPTIONS request correctly, the real request never happens.
Summary points
- GET reads, POST creates, PUT replaces, PATCH updates part, DELETE removes.
- Safe methods change nothing; idempotent methods can be repeated safely.
- Never change data with GET.
- Pair each method with a fitting status code, covered in the next lesson.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.