HTTP Methods
HTTP methods (also called verbs) tell the server what kind of action the client wants. Pick the right one and your API stays predictable.
The common methods
| Method | Purpose | Idempotent? | Safe? |
|---|---|---|---|
GET | Read a resource. | Yes | Yes |
HEAD | Same as GET, but only headers. | Yes | Yes |
OPTIONS | Ask what methods/headers are allowed (used by CORS). | Yes | Yes |
POST | Create a resource or submit data. | No | No |
PUT | Replace a resource entirely. | Yes | No |
PATCH | Partially update a resource. | No | No |
DELETE | Remove a resource. | Yes | No |
Idempotent vs safe
- Safe: the method doesn't change anything on the server. Browsers can prefetch, cache, and replay safely.
- Idempotent: calling it twice has the same effect as calling it once. Allows retry on network failure.
REST in one line
GET /posts # list GET /posts/42 # read one POST /posts # create PUT /posts/42 # replace PATCH /posts/42 # partial update DELETE /posts/42 # delete
Tip: HTML forms only natively support
GET and POST. Frameworks (Laravel, Rails) emulate PUT, PATCH, and DELETE by sending a hidden _method field.Example
Example
<!DOCTYPE html>
<html>
<head>
<title>HTTP Methods</title>
</head>
<body>
<h1>HTTP Methods</h1>
<p>This is a demo page for the "HTTP Methods" lesson.</p>
</body>
</html>
Try it Yourself »
Exercise
Which HTTP method is used to fetch (read) a resource without changing it?
/users
Three letters.
Discussion
Loading…