Signup/Sign In
Ask Question
Not satisfied by the Answer? Still looking for a better solution?

What is the difference between POST and PUT in HTTP?

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

The PUT method requests that the enclosed entity be stored under the supplied Request-URI. If the Request-URI refers to an already existing resource, the enclosed entity SHOULD be considered as a modified version of the one residing on the origin server. If the Request-URI does not point to an existing resource, and that URI is capable of being defined as a new resource by the requesting user agent, the origin server can create the resource with that URI.

So which HTTP method should be used to create a resource? Or should both be supported?
by

2 Answers

akshay1995
PUT /user/12345 HTTP/1.1 <-- create the user providing the id 12345
Host: mydomain.com

GET /user/12345 HTTP/1.1 <-- return that user
Host: mydomain.com
Otherwise, use POST to initially create the object, and PUT to update the object:

POST /user HTTP/1.1 <--- create the user, server returns 12345
Host: mydomain.com

PUT /user/12345 HTTP/1.1 <--- update the user
Host: mydomain.com
sandhya6gczb
POST means "create new" as in "Here is the input for creating a user, create it for me".

PUT means "insert, replace if already exists" as in "Here is the data for user 5".

Login / Signup to Answer the Question.