Why use put over post?
The particular section I want to highlight as a difference is this:
“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 /questions”
The equivalent section in new RFC is at https://tools.ietf.org/html/rfc7231
The meaningful change is: “The POST method requests that the target resource process the representation enclosed in the request according to the resource’s own specific semantics.”
So essentially POST is less prescriptive on usage.
I think what this means is that the semantics of POST to create a sub-resource is still a good pattern to follow, it is an example usage even in the new RFC, however, we shouldn’t try to state that POST means create, that’s just one possibility.
The important points to follow with POST is that if a resource is created then a Location header must be specified and 201 response returned. We can also use hypermedia to describe to clients the behavior of POST.
GET /resource could return
Both PUT and POST can be used for creating.
You have to ask, "what are you performing the action upon?", to distinguish what you should be using. Let's assume you're designing an API for asking questions. If you want to use POST, then you would do that to a list of questions. If you want to use PUT, then you would do that to a particular question.
Great, both can be used, so which one should I use in my RESTful design:
You do not need to support both PUT and POST.
Which you use is up to you. But just remember to use the right one depending on what object you are referencing in the request.
Some considerations:
An example:
I wrote the following as part of another answer on SO regarding this:
For example, if we have to test public APIs or retrieve data from the web server, we can use the “GET” HTTP method. Now, if we want to get an image from a website, then we can use this command:
Apart from the “GET” method, there are so many other HTTP methods. These methods are listed below:
Developers sometimes confuse POST and PUT methods. For example, which method to use where in the code? Or whats an API testing process through POST or PUT?
This article will highlight all those aspects that differentiate HTTP PUT and POST methods. Continue reading!
Before proceeding with the HTTP methods, it is important that we know about REST API
Roy Fielding developed REST which means representational state transfer. REST API also known as RESTful API, is a web application programming interface.
The REST API allows us to interact with RESTful web services. Moreover, it adopts a REST architectural style.
Simply put, we can say that REST helps us to communicate between two computers using HTTP technologies. The HTTP technologies are mostly found in web servers or web browsers.
SOAP vs REST:
People also sometimes confuse REST with SOAP. Therefore, you should know that SOAP has a rigid set of patterns compared to REST.
If we want to update the available resource on the server, the PUT method can be helpful.
Typically, we can say that the PUT method replaces the target URL with something else.
We can use the PUT method to overwrite an existing resource or make a new resource. Some examples of PUT are listed below:
The request is given as under:
If the target resource is modified with the enclosed representation then there should be two responses from the server.
First response = 200(OK)
Second response = 204(No Content)
If there is no representation from the target resource, then the server should inform the user through a 201 (Created) code.
For example:
If we want to send user-generated data to the web server, then the POST HTTP method is useful.
For example, if we upload a profile picture or comment on any forum, it will happen using the POST method.
An important thing to note is that we should use the POST method only when we don’t know the specific URL of your newly created resource.
Let’s suppose we created a forum thread. If the path of the thread is not specified, we can use the following:
This method will give us the URL path from the origin server and a response similar to:
In simple words, we can say that the POST method helps to create subordinates. The request URI identifies the subordinates or child resources.
The differences between these two methods are often asked in REST API interview questions. Some of the key differences between the POST method and the PUT method requests are enlisted below:
while the syntax of the POST method is:
If we talk about general case scenarios then we should use PUT every time when we want to update operations.
On the other hand, we should use POST when we want to create operations.
We also need to use PUT when we want to modify a part of resource collection. On the other hand, if want to add a child resource into resource collection then we should use POST.
Now if we take an example of networking and we need to use PUT and POST requests. Then we can code as under:
PUT:
POST:
We can take another example of put and post that is linked with testing APIs. If we want to test APIs using put or post, we will do it as under:
Testing APIs with PUT:
Testing APIs with POST:
APILayer REST APIs are helpful in creating applications and improving developers’ productivity. We can use it in the following ways:
PUT request URI refers to creating a new resource or modifying the already existing resource.
It sends user-generated data to the server. For example, we can upload profile pictures or comment on any forum using a POST request.
You can use PUT through the following syntax:
The POST method requests that the origin server accept a message in an enclosed entity. In simple words, it helps to create operations.
POST request helps to add a new subordinate to the resource identified by URI.
When you use post request multiple times, it will give you different results.
POST request identifies the resource that will accept the enclosed entity.
PUT and POST requests have lots of similarities certainly when making an HTTP request and both can be meddled with so that one performs the functions of the other. This article revolves around the major differences between PUT and POST Requests.
HTTP PUT is a request method supported by HTTP used by the World Wide Web. The PUT method requests that the enclosed entity be stored under the supplied URI. If the URL refers to an already existing resource, it is modified and if the URI does not point to an existing resource, then the server can create the resource with that URI.
Example: Let’s try making a request to httpbin’s APIs for example purposes.
save this file as request.py and through the terminal run,
Output:
HTTP POST is a request method supported by HTTP used by the World Wide Web. By design, the POST request method requests that a web server accepts the data enclosed in the body of the request message, most likely for storing it. It is often used when uploading a file or when submitting a completed web form.
Example: Let’s try making a request to httpbin’s APIs for example purposes.
save this file as request.py and through the terminal run,
Output:
The PUT method is used to modify a single resource. The POST method is used to add a child resource. It can be cached. It cannot be cached.