What is POST?
The REST (REpresentational State Transfer) protocol was designed to standardize HTTP communication between clients and servers. REST is a system of rules and connectors all services must follow and use. If you’ve ever had to order a special dongle for that one piece of hardware without a USB connector, you understand the problem that REST solves.
REST APIs perform specific methods of data operations across HTTP:
- GET - This operation reads information from a record in the database
- PUT- This operation changes a record’s information in the database
- POST - This operation creates a new record in the database
- PATCH - This operation updates an existing resource, but does not require sending the entire body with the request
- DELETE - This operation removes a record from the database
In this article we are focusing on the POST operation. What does POST do, and how is it different from PATCH and PUT? In the words of the RFC2616 memo, "the POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line. It essentially means that POST request-URI should be of a collection URI."
POST API Example
A POST request sends data to an API, either creating or updating an existing resource. POST is like a contact form on a website. When you fill out the inputs in a form and hit Send, that data is put in the response body of the request and sent to the server. This may be JSON, XML, or.
A POST request might look like this: `POST https://www.abstractapi.com/users/{{userID}}`. This would send data to the `/users/{{userID}}` endpoint, and either update that user's information or create a new user account.
The response SHOULD be HTTP response code `201`, but sometimes the action performed by the POST method might not result in a resource that can be identified by a URI. In this case, either HTTP response code `200` (OK) or `204` (No Content) is the appropriate response status.
What is the Difference between PUT, PATCH, and POST?
Maybe you looked at the list of operations above and wondered what the differences are between POST, PUT, and PATCH. We use them for different situations, depending on idempotentcy. If a request is idempotent, calling it once or several times successively has the same effect. If the request is non-idempotent, each successive request will act on the previous request, causing potential problems.
How is POST different from PUT?
The difference between PUT vs POST is that PUT is idempotent: calling it once or several times successively has the same effect, whereas successive identical POST requests may have additional effects, akin to placing an order several times.
How is POST Different from PATCH?
PATCH is used less frequently than POST. PATCH applies only partial modification to a resource, unlike POST and PUT, which modify the entire resource. PATCH is non-idempotent, while PUT is idempotent.
Using the `POST https://www.abstractapi.com/users/{{userID}}` example from above, if a user wanted to change their username, a POST or PUT request would send the entire user entity, while PATCH could send just the updated username.
Conclusion
Becoming confident in request methods is an important step in your programming journey. They might seem a little confusing in the beginning, but knowing when to use a POST, a PUT, or a PATCH will make you use resources more efficiently. You might not need to understand the deep mathematical complexities of idempotency, but it's important to know how you are modifying a record with POST, PUT, or PATCH.