LearnThatStack Ace your next interview
API Design · question
Question 3 of 92

Explain the difference between PUT and POST methods.

beginner
← All API Design questions
Re-explain
Visual ↓

POST asks the server to create a new resource and choose its identifier. PUT creates or updates the resource at an identifier the client provides. So POST /users creates a new user, while PUT /users/123 creates or updates the user with ID 123.

The other difference is what a repeat does. POST is not idempotent, because multiple calls create multiple resources. PUT is idempotent, because multiple identical calls have the same effect as one.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for API Design One question at a time. Answer out loud, get graded, see what you missed.
Related concept

HTTP methods: what each verb promises

A method's promise about safety and idempotency decides when a retry is safe, when a cache is legal, and PUT versus POST.

For this question · chapters 1 and 3 of 3The difference between PUT and POST is one lamp on row three, the idempotency guarantee: PUT is idempotent, POST is not. Watch that lamp. The next chapter counts what that lamp costs when a reply is lost and the client retries.

1/3 Verbs have a meaning and a code
httpVerbs have a meaning and a codetwo questions for every verbmeaningsafeidempotentsets the idsuccess codeGETPOSTPOSTPUTPATCHDELETEreadscreatesreplaceschanges fieldsremovesyesnonononoyesnoyesdependsyesclientserverclientclientclient200201201200 or 204200204
httpVerbs have a meaning and a codemeaningsafeidempotentid set bycodeGETPOSTPOSTPUTPATCHDELETEreadcreatereplacechangeremoveyesnonononoyesnoyesdependsyesclientserverclientclientclient200201201200/204200204

Verbs have a meaning and a code

  1. Every HTTP verb is a promise to the client about what a call does. A broken promise means a retry duplicates a charge, or a cache stores an answer that should never be cached. Each verb has a plain meaning: GET reads and POST creates. PUT replaces the whole resource, PATCH changes only the fields in the request, and DELETE removes.
  2. Row two asks whether the call changes anything on the server. Only GET is safe, because GET changes nothing. The other four methods can write. A safe method is one you can call without changing anything, so a browser may prefetch a GET but not a POST.
  3. The second question, in row three, is whether the method is idempotent. Idempotent means a repeated call leaves the server in the same state. GET, PUT and DELETE are idempotent, but POST is not. PATCH depends on what the patch body does, so no one can promise in advance that a PATCH is idempotent.
  4. The fourth question is who sets the id. The server picks the id for POST, so the client cannot know the address before sending the request. The client sets the target for PUT, PATCH and DELETE, and GET reads an id the client already knows. The client already knows the target, so the client can repeat a PUT without checking whether the first one worked.
  5. Row five is the expected success code, the other half of the contract. GET answers 200 and POST answers 201, because something new now exists. PUT answers 200 or 204, PATCH answers 200, and DELETE answers 204. A 204 means the work is done and there is nothing to send back.
  6. Read the POST column top to bottom: it's not safe, not idempotent, and server picks an id. The bottom of the column is the status code 201. POST is the only verb with every guarantee switched off, so you have to protect POST yourself.
Chapter 1 · step 1 of 6

Every HTTP verb is a promise to the client about what a call does. A broken promise means a retry duplicates a charge, or a cache stores an answer that should never be cached. Each verb has a plain meaning: GET reads and POST creates. PUT replaces the whole resource, PATCH changes only the fields in the request, and DELETE removes.

1 of 6 Use ← → or swipe See the concept: all 3 chapters in it

© LearnThatStack - diagrams may not be republished without permission.

How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • When would you use PATCH instead of PUT? PUT sends the full replacement, so a missing field gets dropped. PATCH sends only the change and is not guaranteed to be idempotent.
  • How would you make a POST that charges a card safe to retry? Name a client-generated idempotency key. The server stores the first result under that key and returns it on a repeat instead of charging twice.
  • Which status codes should PUT and POST return when they succeed? A POST that creates returns 201 with a Location header. PUT returns 201 when it creates and 200 or 204 when it replaces.
  • Two clients PUT different versions of /users/123 at the same time. What happens, and how do you prevent it? The last write wins and one update is lost. A strong answer uses an ETag with If-Match, so a stale write gets 412 Precondition Failed.
  • PUT is idempotent. Is it also safe, and what is the difference? Safe means the request changes nothing, like GET. PUT changes state, so it is idempotent but not safe. Repeating it leaves the same end state.

What you can say

  1. POST asks the server to create something new. PUT puts a resource at a URL the client already knows.
  2. With POST /users the server picks the new user's ID, so sending the same request twice creates two users. That's why POST is not idempotent.
  3. With PUT /users/123 the client names the ID and sends the resource, and the server either creates it or replaces what's already there.
  4. PUT is idempotent. Sending the same PUT ten times leaves the server in the same state as sending it once.
  5. That matters for retries. A client can resend a PUT after a timeout without harm, but a resent POST can create a duplicate unless you add an idempotency key.
  6. The trade-off is who owns the ID. PUT only works when the client knows the ID up front, so when the server should assign IDs I use POST and deal with duplicate retries separately.

Weak answers to avoid

  • Says POST creates and PUT updates, nothing more PUT can create too, at a URL the client picks. The question is testing idempotency and who assigns the ID, so talk about both.
  • Treats PUT as a partial update PUT replaces the whole resource, so leaving out a field can wipe it. Partial changes belong in PATCH.
  • Can't say why idempotency matters Tie it to retries. A client can resend a PUT after a timeout safely, but a resent POST can create a duplicate order or a second charge.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the API Design cheatsheet.

← Back to all API Design questions
Pro · $10/mo

74 of 92 API Design answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime