PUT vs POST

Contents
The short answer
Who chooses the URL
Idempotence
Both have a body
An example: Swagger Petstore
PATCH
In an interview

The question comes up in interviews and when you design an API. The meaning of the methods is defined in RFC 9110, HTTP Semantics; an API can still use them its own way, so the documentation of a given API has the last word.

The short answer

PUT puts the sent data at the URL of the request: it creates the resource there or replaces it completely. Sending the same PUT twice leaves the same state as sending it once.

POST hands the sent data to the resource at the URL, and that resource decides what to do with it. Most often it creates a new item and the server chooses its address. Sending the same POST twice can create two items.

Who chooses the URL

With PUT the client knows the final address:

curl -X PUT -H 'Content-Type: application/json' -d '{"name": "Rex", "status": "available"}' https://api.example.com/pets/42

With POST the client sends to a collection, and the server answers with the address it gave the new item, usually as 201 Created with a Location header:

curl -i -X POST -H 'Content-Type: application/json' -d '{"name": "Rex", "status": "available"}' https://api.example.com/pets

The codes are explained in HTTP status codes.

Idempotence

A method is idempotent when several identical requests have the same effect on the server as one. PUT, DELETE and of course GET are idempotent, POST is not. This matters on a bad network: a client may safely repeat a PUT whose answer got lost, but a repeated POST may order the same goods twice.

Both have a body

A common myth says that PUT puts everything into the URL and has no body. It is the other way round: the body of a PUT is the new state of the resource. Both PUT and POST normally carry a body, in JSON, a form or any other format. For GET, HEAD and DELETE a body has no defined meaning, and servers may ignore or reject it.

An example: Swagger Petstore

The sample API of the Swagger Editor shows both styles. POST /pet adds a new pet, PUT /pet updates an existing one:

Swagger Petstore: POST /pet adds a pet, PUT /pet updates a pet
POST adds, PUT updates

Further down, POST /pet/{petId} updates a pet with form data. So POST can update too; it is the resource that decides what a POST does:

Swagger Petstore: POST /pet/{petId} updates a pet with form data
POST that updates

PATCH

PUT replaces the whole resource: fields left out of the body are gone afterwards, or set to their defaults. To change one field, APIs use PATCH, which sends only the changes:

curl -X PATCH -H 'Content-Type: application/json' -d '{"status": "sold"}' https://api.example.com/pets/42

In an interview

To try the methods yourself, see API testing. To see a request as plain text on the wire, type it by hand over telnet.

Article authors: Andrei Olegovich, Klavdiya Kondakova

Related articles
Responsive design: an adaptive menu
Beget mail: create a mailbox
Canonical page: rel=canonical
CSS
Firefox
Which hosting to choose
VPS hosting
HTTP status codes
Joomla
OpenCart
PUT vs POST
Redirects
Russian IP ranges
SEO
Element tbody not allowed as child of element table in this context
CSS test: shapes and line numbers
CSS test 2: the code blocks of this site
Website testing
A tram drawn with CSS
URL encoding
UTM tags
Telling bots from people
WebP images
Moving a website: domain and hosting
WordPress
Move a WordPress site
Yandex Metrica: goals
Yandex Mail: collect mail from other mailboxes
Banner image

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: