来自: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. WebService 调用

    一.WebService调用 1.webservice支持四种调用方式:SOAP 1.1,SOAP 1.2,GET,POST.           2.如果要webservice支持GET,POST调 ...

  2. DataTable与List互换

    public static class List2DataTable { #region "Convert Generic List to DataTable" /// <s ...

  3. ASP.NET MVC 下拉框的传值的两种方式

    以前使用WebForm变成时,下拉框传值只需直接在后台绑定代码就可以了.现在我们来看看在MVC中DropDownList是如果和接受从Controller传过来的值的. 第一种:使用DropDownL ...

  4. Eclipse格式化代码快捷键失效问题

    一般情况下,Eclipse快捷键失效是因为与其它软件快捷键冲突,Eclipse格式化代码快捷键正好与搜狗输入法的“简繁切换”的快捷键冲突,将搜狗输入法的快捷键修改一下就行了.

  5. (Gym 100685G) Gadget Hackwrench(LCA在线ST)

    Gadget Hackwrench time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...

  6. 无线安全专题01--kali破解WPA

    最近一直用flask写自己的博客框架,等过段时间才能将其分享出来,所以在此期间决定开个无线安全的专题,分享一些无线安全方面的知识. 好了,废话不多说,咱们进入今天的主题,使用kali linux上的无 ...

  7. 《Linux内核设计与实现》读书笔记(二十)- 补丁, 开发和社区

    linux最吸引我的地方之一就是它拥有一个高手云集的社区, 还有就是如果能=为linux内核中贡献代码, 一定是一件令人自豪的事情. 下面主要总结一些和贡献代码相关的主要内容. 加入社区 编码风格 提 ...

  8. Caliburn.Micro(CM) 穿过 Popup 绑定方法

    今天一个朋友,在用CM框架中,在一个ListView的DataTemplate,中用了个Popup,发现绑定不到VM(集合外的VM,即ListView的DataContext)中的方法了.我查了一下C ...

  9. [DNX]解决dnu restore时找不到Newtonsoft.Json的问题

    在Mac上用最新版的dnx 1.0.0-beta5-11855进行dnu restore,出现下面的错误: System.IO.FileNotFoundException: Could not loa ...

  10. [WinAPI] API 13 [遍历指定目录 打印文件和其他属性]

    Windows API中,有一组专门的函数和结构,用于遍历目录,它们是FindFirstFile函数.FindNextFile函数和WIN32_FIND_DATA结构.使用FindFirstFile和 ...