來自:http://stackoverflow.com/questions/630453/put-vs-post-in-restrestful
http://www.15yan.com/story/7dz6oXiSHeq/ide
Overall:post
Both PUT and POST can be used for creating.ui
You have to ask "what are you performing the action to?" 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.this
Great both can be used, so which one should I use in my RESTful design:url
You do not need to support both PUT and POST.rest
Which is used is left up to you. But just remember to use the right one depending on what object you are referencing in the request.code
Some considerations:orm
An example:server
I wrote the following as part of another answer on SO regarding this:
POST:
Used to modify and update a resource
POST /questions/<existing_question> HTTP/1.1 Host: www.example.com/Note that the following is an error:
POST /questions/<new_question> HTTP/1.1 Host: www.example.com/If the URL is not yet created, you should not be using POST to create it while specifying the name. This should result in a 'resource not found' error because
<new_question>
does not exist yet. You should PUT the<new_question>
resource on the server first.You could though do something like this to create a resources using POST:
POST /questions HTTP/1.1 Host: www.example.com/Note that in this case the resource name is not specified, the new objects URL path would be returned to you.
PUT:
Used to create a resource, or overwrite it. While you specify the resources new URL.
For a new resource:
PUT /questions/<new_question> HTTP/1.1 Host: www.example.com/To overwrite an existing resource:
PUT /questions/<existing_question> HTTP/1.1 Host: www.example.com/