3 ms·
Both POST and PUT can be used to create and update info, the difference is idempotence. In other words, making the same PUT request over and over again won't ch
by brousky 14y ago
Both POST and PUT can be used to create and update info, the difference is idempotence. In other words, making the same PUT request over and over again won't change the result beyond the initial request.
For example:
- Repeat "POST /entries" 5 times with the same request body and you'll have 5 new and identical entries on the server.
- Repeat "PUT /entries" 5 times with the same request body and you'll overwrite the set of entries on the server 5 times.
- Repeat "POST /entries/1234" 5 times and you'll have 5 times whatever the server says it will do when you POST on a given entry (eg. if the server keeps a modify count on that entry, it will end up incremented by 5)
- Repeat "PUT /entries/1234" 5 times and you'll overwrite that entry 5 times on the server. The end-result will be exactly the same as if you did the request 1 or 100 times, including any counters that are part of the entry itself (because those counters would be part of that PUT request body, see below).
Also, a PUT request is usually made on a specific, unique resource unless you want to overwrite a complete set. If the id specified in the URL doesn't yet exist, it will be created. The request body includes the complete resource data to be created/overwritten. Think file uploads.
A POST request can either create a resource or update parts of an existing resource based on the parameters given in the request body. When it creates, the server assigns the id and creates the URL of the newly created resource. Whether it creates or updates depends on the URL you make the POST request to identifies a specific item or not.
[EDIT: clarified a few things about PUT on an id vs a set)