# HTTP methods

> GET, POST, PUT, PATCH, DELETE: the verbs of HTTP.

Source: https://aminbytes.dev/bytes/http/requests/http-methods/


The first word of every request is the **method**. It says *what you want to do* with the resource in the path. (Side note: There are a few more, like `HEAD` (only the headers, no body) and `OPTIONS` ("what am I allowed to do here?"), which browsers send for cross-site requests.)

```http
DELETE /users/42 HTTP/1.1
Host: api.example.com
```

## The common ones

| Method     | Meaning                       | Has a body? | Safe | Idempotent |
| ---------- | ----------------------------- | ----------- | ---- | ---------- |
| **GET**    | Read a resource               | No          | ✅   | ✅         |
| **POST**   | Create something / do an action | Yes       | ❌   | ❌         |
| **PUT**    | Replace a resource completely | Yes         | ❌   | ✅         |
| **PATCH**  | Change part of a resource     | Yes         | ❌   | ❌         |
| **DELETE** | Remove a resource             | Usually not | ❌   | ✅         |

- **Safe** means it doesn't change anything on the server. Browsers and crawlers may send safe requests freely, so never let a `GET` delete data.
- **Idempotent** means sending it twice has the same effect as sending it once. Deleting user 42 twice still leaves user 42 deleted. That makes it safe to **retry** after a network error.

> [!IMPORTANT]
> A `POST` is *not* idempotent: retrying "place order" can create two orders. That's why browsers ask "Confirm form resubmission?" when you reload after submitting a form.

## A small REST example

APIs often combine methods with paths like this (a style called REST):

```text
GET    /todos       → list all todos
POST   /todos       → create a todo
GET    /todos/7     → read todo 7
PATCH  /todos/7     → update part of todo 7
DELETE /todos/7     → delete todo 7
```

## Try them on a pretend API

Pick a method, press **Send**, and read the raw request and response. Try `POST` twice, `DELETE` twice, and a `PATCH` without logging in.

*[Interactive example: try it at https://aminbytes.dev/bytes/http/requests/http-methods/]*

## Trying them with curl

`curl` sends `GET` by default. Use `-X` to pick another method:

```bash
curl https://jsonplaceholder.typicode.com/todos/1
curl -X DELETE https://jsonplaceholder.typicode.com/todos/1
```

And the same from JavaScript with `fetch`:

```js {title="methods.js" hl_lines="2"}
const res = await fetch("https://jsonplaceholder.typicode.com/todos/1", {
  method: "PATCH",
  headers: { "Content-Type": "application/json" },
  body: JSON.stringify({ completed: true }),
});

console.log(res.status);      // 200
console.log(await res.json()); // the updated todo
```

> [!NOTE]
> [JSONPlaceholder](https://jsonplaceholder.typicode.com) is a free fake API for practice. It answers like a real one but doesn't actually save your changes.


## Byte check

1. Which method should **never** change data on the server?
   - **`GET`** (correct)
   - `POST`
   - `DELETE`
   `GET` is *safe*: browsers and crawlers may send it freely, so it must not delete or change anything.

2. You send `DELETE /users/42` twice. Why is that fine?
   - The browser drops the second one
   - **`DELETE` is idempotent: twice has the same effect as once** (correct)
   - Servers block repeated requests
   User 42 is gone either way. That's what makes idempotent requests safe to retry.

3. Which request could create **two** orders if you retry it after a network error?
   - `PUT /orders/7`
   - **`POST /orders`** (correct)
   - `GET /orders`
   `POST` isn't idempotent: every one can create something new. That's why browsers warn before resubmitting a form.


