Coding

PUT vs POST: Differences, Idempotency and When to Use Each

Use PUT when you know the exact URL of the resource and you are sending its complete new state: the server creates it or replaces it, and sending the same request twice leaves the same result. Use POST when you are handing data to a resource to process, for example to create a new item whose URL the server chooses, and accept that repeating the request may repeat the effect.

PUT vs POST at a glance

PUTPOST
Meaning (RFC 9110)The enclosed representation replaces the state of the target resource, or creates itThe target resource handles the enclosed representation according to its own semantics
Who picks the URLThe client: PUT presumes the client knows which resource it wantsUsually the server, for new resources
IdempotentYesNo
Safe (read-only)NoNo
Cacheable responseNoOnly with explicit freshness information
Typical success code201 if created; 200 or 204 if replaced200, or 201 with a Location header if it created something
Allowed in HTML formsNoYes
Safe to retry automaticallyYes, because it is idempotentNot unless the client knows it is safe

The core difference: who decides the URL

RFC 9110 puts it this way: in a POST, the target resource is intended to handle the enclosed representation. In a PUT, the enclosed representation is defined as replacing the state of the target resource. That is why PUT presumes the client already knows which resource it wants. If a service chooses the URI for the client, the RFC says to use POST instead.

Two example requests (example syntax, not output from a real server). Creating an order where the server assigns the ID:

POST /orders HTTP/1.1
Host: api.example.com
Content-Type: application/json

{"item":"book","qty":1}

A well-behaved server answers 201 Created with a Location: /orders/4821 header. Send the same request again and you get a second order, /orders/4822. Now storing a document at a URL the client chose:

PUT /users/alex/avatar.png HTTP/1.1
Host: api.example.com
Content-Type: image/png

(binary image data)

The first time, there is no current representation, so the server creates one and must send 201 Created. The second time, the server replaces it and must send 200 OK or 204 No Content. Either way, /users/alex/avatar.png now holds that image, and a later GET should return an equivalent representation.

What idempotent means, and why it matters

An idempotent method has the same intended effect whether you send the request once or several times. RFC 9110 lists PUT, DELETE and all safe methods as idempotent. POST is not on the list. MDN's example of the consequence: successive identical POST requests may have additional effects, such as creating the same order several times.

The practical benefit shows up when a connection drops and you do not know whether the request arrived. A client or library can resend a PUT without risk. Resending a POST might create a duplicate, so RFC 9110 says a client should not automatically retry one unless it knows the request is safe to repeat.

Idempotency is about the intended effect on the resource, not about identical responses. The first PUT returns 201 and the second returns 200 or 204, and the method is still idempotent.

PUT replaces the whole resource

PUT is a replacement, not a merge. If a user record has name, email and phone, and you PUT only {"email":"[email protected]"} to it, a server that follows the definition replaces the stored state with that one field. The other two fields are gone or reset. RFC 5789 says PUT is defined to overwrite a resource with a complete new body.

For a partial update, the standard tool is PATCH. It carries a description of changes to apply rather than a new copy. RFC 5789 states PATCH is neither safe nor idempotent by default, though a client can issue it in a way that is idempotent, and it recommends conditional requests such as If-Match with an ETag to prevent collisions.

RFC 9110 also describes a partial PUT: some servers accept it when the client sends a Content-Range header. The RFC says support is inconsistent and depends on private agreements, and that partial PUT is not backwards compatible with the original definition. A server that does not support it should respond with 400 (Bad Request). Do not rely on it for a public API.

Status codes to return

  • PUT created the resource: 201 (Created). RFC 9110 says the origin server must send it.
  • PUT replaced an existing resource: 200 (OK) or 204 (No Content). Also required by the RFC.
  • POST created a resource: 201 (Created) with a Location header pointing at it. See HTTP status codes explained.
  • POST performed some other action: 200 (OK) with a result, or another code that fits.

Caching and forms

MDN lists PUT as not cacheable. POST responses are cacheable only when they carry explicit freshness information, and most caches never store them. Neither method is a good fit for data you want a CDN to serve; that is a job for GET.

HTML forms support only GET and POST, and MDN lists PUT as not allowed in forms. A browser sends PUT only from script, for example with fetch:

await fetch("/users/alex/avatar.png", {
  method: "PUT",
  headers: { "Content-Type": "image/png" },
  body: imageBlob,
});

Which to use: quick decision rules

  • Client knows the URL and sends the full new state: PUT. Uploading a file to a known path and saving a settings document are typical cases.
  • Server assigns the ID or URL of the new item: POST to the collection, such as /orders.
  • Triggering an action (send an email, start a job, process a payment): POST. These are not replacements of a resource.
  • Changing one or two fields: PATCH, or PUT with the full record.
  • Need safe retries on a POST anyway: many APIs accept a client-generated idempotency key. That is an API convention, not part of the HTTP standard, so check the API's documentation. A random ID from a UUID generator is the usual format for the key.

Frequently asked questions

Is PUT used to create or to update?
Both. RFC 9110 says PUT requests that the state of the target resource be created or replaced. If nothing exists at the URL, the server creates it and answers 201 (Created). If something exists, the server replaces it and answers 200 (OK) or 204 (No Content).
Can I use POST to update a resource?
HTTP allows it, because POST just asks the target resource to process the enclosed data. Many APIs use POST for updates and actions. The cost is that clients and proxies cannot assume a repeated POST is harmless, so they will not retry it automatically.
Is PUT or POST better for a REST API?
Neither is better. Use PUT when the client knows the exact URL and sends the full new state of that resource. Use POST when the server picks the new resource's URL, or when the request is an action rather than a replacement.
Does PUT work in an HTML form?
No. MDN lists PUT as not allowed in HTML forms, which support only GET and POST. Browser code can send PUT with fetch or XMLHttpRequest.
What should I use for a partial update?
PATCH, defined in RFC 5789. PUT is defined to overwrite a resource with a complete new body. PATCH carries a set of changes to apply to the current resource.

Sources