🌐

REST + fetch()

HTTP methods, fetch GET/POST patterns. Memorize the two-.then() chain.

HTTP methods

MethodPurpose
GETRead data (no body)
POSTCreate new data (with body)
PUTUpdate / replace
DELETERemove

fetch — GET

fetch("https://api.example.com/data")
    .then(res => res.json())
    .then(data => {
        console.log(data);
    })
    .catch(err => console.log("Error:", err));

fetch — POST

fetch("https://api.example.com/data", {
    method: "POST",
    headers: { "Content-Type": "application/json" },
    body: JSON.stringify({ name: "Ali", age: 25 })
})
    .then(res => res.json())
    .then(data => alert("Saved!"));

Line-by-line meaning

Status codes

200 OK · 201 Created · 400 Bad request · 404 Not found · 500 Server error