来自:http://stackoverflow.com/questions/630453/put-vs-post-in-rest

http://www.15yan.com/story/7dz6oXiSHeq/

Overall:

Both PUT and POST can be used for creating.

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.

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 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.

Some considerations:

  • Do you name your URL objects you create explicitly, or let the server decide? If you name them then use PUT. If you let the server decide then use POST.
  • PUT is idempotent, so if you PUT an object twice, it has no effect. This is a nice property, so I would use PUT when possible.
  • You can update or create a resource with PUT with the same object URL
  • With POST you can have 2 requests coming in at the same time making modifications to a URL, and they may update different parts of the object.

An example:

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/

随机推荐

  1. mybatis 控制台打印sql

    开发时调试使用 <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBe ...

  2. ubuntu下配置lamp环境

    安装MySQL sudo apt-get install mysql-server mysql-client 安装php模块 Sudo apt-get install php5 安装Apache2 S ...

  3. [转载]tail No space left on device

    转载http://www.chenxie.org/?p=717 # tail -f ../logs/catalina.outtail: cannot watch `../logs/catalina.o ...

  4. C++通过模板实现多态

    #include <iostream> using namespace std; template<typename T> class Phone { public: void ...

  5. 【Java学习笔记】Map集合的keySet,entrySet,values的用法例子

    import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.M ...

  6. struts2 学习记录 过滤器 国际化

    struts2接触不是一天两天了,但是一直没有用它做什么项目,但老师确一直说它有很大的学习价值,所以还是把我学习到的东西给记录一下,记录的东西没有规律,只是给自己留个备份, struts2中最关键的是 ...

  7. JAVA_BaseDAO数据处理类

    package dao; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStat ...

  8. day4----装饰器

    装饰器本质是函数,装饰其他函数,就是为其他函数添加附加功能. 原则:1.不能修改被装饰的函数的源代码           2.不能修改被装饰的函数的调用方式   实现装饰器 知识储备 1.函数即“变量 ...

  9. Webpack使用教程五(Babel)

    Babel是一个JavaScript编译和工具平台,使用Babel我们可以:使用新版本的JavaScript(ES6/ES2015,ES7/ES2016),尽管有些浏览器不能全部支持新特性:使用Jav ...

  10. sqlplus启动后的环境SQLPATH的设置

    sqlplus启动时会查找和加载的两个文件login.sql和glogin.sql.其中glogin.sql文件默认存放在$ORACLE_HOME/sqlplus/admin目录下,login.sql ...