Beginner · How the web and backends work · topic 3 of 54 · 25 min
HTTP requests, responses and methods
Explanation
What a request looks like
HTTP (HyperText Transfer Protocol) messages are plain text with a fixed shape. A request has a start line, then headers, then an empty line, then an optional body. Headers are extra details written as name: value. The body is the data you send, like a form or JSON.
POST /orders HTTP/1.1
Host: api.chaiwala.in
Content-Type: application/json
{"item": "Masala chai", "qty": 2}The response has the same shape. Its first line is a status line with a status code (a number like 200 or 404) and a short reason. Then come headers and the body.
HTTP/1.1 201 Created
Content-Type: application/json
{"id": 501, "item": "Masala chai", "qty": 2, "total": 40}The five main methods
| Method | Used to | Example | Has a body? |
|---|---|---|---|
| GET | Read data | GET /products/42 | No |
| POST | Create something new | POST /orders | Yes |
| PUT | Replace a whole thing | PUT /users/7 with the full user | Yes |
| PATCH | Change a few fields | PATCH /users/7 with {"city": "Pune"} | Yes |
| DELETE | Remove something | DELETE /cart/items/3 | Usually no |
Safe and idempotent
A safe method only reads; it never changes data on the server. GET and HEAD are safe. That is why browsers and search engines feel free to send GET requests at any time.
An idempotent method gives the same end result whether you send it once or five times. GET, PUT and DELETE are idempotent: deleting item 3 twice still leaves item 3 deleted. POST is not idempotent: sending "create order" twice makes two orders. PATCH is usually treated as not idempotent too.
const cart = { rice: 1 };
// PUT-like: set the value. Doing it twice gives the same result.
function putQty(item, qty) { cart[item] = qty; }
putQty('sugar', 2);
putQty('sugar', 2);
console.log(cart.sugar); // 2
// POST-like: add one more. Doing it twice changes the result again.
const orders = [];
function postOrder(item) { orders.push({ id: orders.length + 1, item }); }
postOrder('chai');
postOrder('chai');
console.log(orders.length); // 2 orders, not 1Watch the lesson
HTTP requests, responses and methods · 1/6
HTTP is the language browsers and servers speak. Every message has the same shape.
Prefer a video by a teacher? Find free videos on “http methods get post put patch delete explained for beginners” on YouTube ↗
In real life: Managing a cart on a shopping app
When you open your cart, the app sends GET /cart. When you add a phone cover, it sends POST /cart/items with the product in the body. Changing the quantity from 1 to 2 sends PATCH /cart/items/9 with {"qty": 2}. Removing it sends DELETE /cart/items/9. The server looks at the method first to know what to do.
const requests = [
{ method: 'GET', path: '/cart' },
{ method: 'POST', path: '/cart/items', body: { productId: 31, qty: 1 } },
{ method: 'PATCH', path: '/cart/items/9', body: { qty: 2 } },
{ method: 'DELETE', path: '/cart/items/9' },
];
for (const r of requests) console.log(r.method, r.path);Practice (0/3 done)
Write a function methodFor(action) that returns the HTTP method for an action: "list" and "read" give "GET", "create" gives "POST", "replace" gives "PUT", "edit" gives "PATCH", "remove" gives "DELETE". Any other action gives null.
Write a function buildRequest(method, path, body) that returns { method, path, headers, body }. The method must be in capital letters. When body is given, headers is { "Content-Type": "application/json" } and body is the JSON text of it. When body is not given, headers is {} and body is null.
Write a function describeMethod(method) that returns { safe, idempotent } for a method name in any case. GET and HEAD are safe and idempotent. PUT and DELETE are idempotent but not safe. POST and PATCH are neither.
Quick quiz
Read more (free)
Pass the quiz (60% or more) or mark this topic as done when you are ready.
Your progress is saved on this device only.