Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Networking 3: DNS, HTTP and HTTPS

    HTTP Methods: Safe and Idempotent

    Safe — doesn't change anything on the server. Only reads.

    Chapter 4 of 135 min read

    The Plain Picture#

    At a library counter you can: look at a book (changes nothing), donate a new book, replace a whole book with a new edition, correct one page, or remove a book. Some actions are harmless to repeat — looking twice changes nothing, removing the same book twice leaves it just as removed. Others aren't — donating twice means two copies.

    The Real Names#

    MethodMeaningStudyBuddy exampleSafe?Idempotent?
    GETread a resourceGET /notes/42✅✅
    POSTcreate something / trigger an actionPOST /notes (new note)❌❌
    PUTreplace a resource completelyPUT /notes/42 (whole note)❌✅
    PATCHchange part of a resourcePATCH /notes/42 (just the title)❌❌ (usually)
    DELETEremove a resourceDELETE /notes/42❌✅
    HEADlike GET, but headers onlycheck if a file changed✅✅
    OPTIONS"what am I allowed to do here?"used by browsers for CORS (Security Fundamentals)✅✅
    • Safe = doesn't change anything on the server. Only reads.

    • Idempotent = doing it once or ten times leaves the server in the same state. DELETE /notes/42 five times → the note is gone (the later calls may return 404, but the state is the same). POST /notes five times → five notes.

    Under the Hood — why idempotency matters so much#

    Networks fail in the worst possible place (Networking 2: TCP, UDP and Ports Ch 6: an ACK doesn't mean the program finished). Picture this:

      App ── POST /payments {amount: 499} ──▶ Server charges the card ✔
      App ◀──────── response lost (Wi-Fi drops) ──────── ✗
      App: "no reply... retry?"
    • If the request were idempotent, retrying is safe.

    • It's a POST — retrying may charge Priya twice.

    The standard fix is an idempotency key: the client generates a unique ID once and sends it with every retry (Idempotency-Key: 7f3c…). The server remembers which keys it already processed and returns the saved result instead of charging again. Stripe's API works exactly this way. You'll build this in How Web APIs Work (coming soon).

    Rule of thumb: clients, proxies and load balancers may automatically retry idempotent requests; they must not blindly retry POSTs.

    Gotchas

    • Never change data in a GET. Browsers prefetch links, crawlers follow them, caches replay them. A GET /notes/42/delete link will eventually be "clicked" by a bot.

    • PATCH vs PUT: a PUT that leaves out a field should wipe that field (it replaces the whole thing); a PATCH only touches the fields sent. Many real APIs blur this — be explicit in your API docs.

    Check yourself

    4 questions
    1

    What's the difference between "safe" and "idempotent"?

    Show answer

    Safe = changes nothing. Idempotent = repeating it has the same effect on server state as doing it once (it may change things — the first time).

    2

    Is DELETE idempotent? Is POST?

    Show answer

    DELETE yes; POST no.

    3

    A payment POST times out. Why is a blind retry dangerous, and what's the standard fix?

    Show answer

    The first request may have succeeded — the retry could charge twice. Send an idempotency key so the server can detect repeats.

    4

    Why should a GET never change data?

    Show answer

    Browsers, crawlers and caches issue GETs freely and repeatedly, assuming they're harmless.