Menu

HTTP methods

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

View .md

The first word of every request is the method. It says what you want to do with the resource in the path.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#

MethodMeaningHas a body?SafeIdempotent
GETRead a resourceNo✅✅
POSTCreate something / do an actionYes❌❌
PUTReplace a resource completelyYes❌✅
PATCHChange part of a resourceYes❌❌
DELETERemove a resourceUsually 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.

Try it Send a request to a pretend API It runs in your browser and nothing leaves your computer, so break whatever you like.
api.example.com
Paths:

Request

Response

Press Send to see the answer.

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:

methods.jsjs
1const res = await fetch("https://jsonplaceholder.typicode.com/todos/1", {
2  method: "PATCH",
3  headers: { "Content-Type": "application/json" },
4  body: JSON.stringify({ completed: true }),
5});
6
7console.log(res.status);      // 200
8console.log(await res.json()); // the updated todo

Note

JSONPlaceholder is a free fake API for practice. It answers like a real one but doesn’t actually save your changes.

Byte check

0/3

Answer these 3 questions to commit this byte to your learning path.

  1. Which method should never change data on the server?
  2. You send DELETE /users/42 twice. Why is that fine?
  3. Which request could create two orders if you retry it after a network error?
Search posts and bytes

Optional account

Keep your progress everywhere

Everything on AminBytes is free to read, with or without an account. Sign in only if you want your commits, streak and merged topics saved, on every device.

or

We keep only your name, avatar and which bytes you finished, and you can delete it all anytime. Privacy