博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
When should we use PUT and when should we use POST?
阅读量:1970 次
发布时间:2019-04-27

本文共 1615 字,大约阅读时间需要 5 分钟。

When should we use PUT and when should we use POST?

The HTTP methods POST and PUT aren't the HTTP equivalent of the CRUD's create and update. They both serve a different purpose. It's quite possible, valid and even preferred in some occasions, to use POST to create resources, or use PUT to update resources.

Use PUT when you can update a resource completely through a specific resource. For instance, if you know that an article resides at http://example.org/article/1234, you can PUT a new resource representation of this article directly through a PUT on this URL.

If you do not know the actual resource location, for instance, when you add a new article, but do not have any idea where to store it, you can POST it to an URL, and let the server decide the actual URL.

PUT /article/1234 HTTP/1.1
red stapler
12.50
POST /articles HTTP/1.1
blue stapler
7.50
HTTP/1.1 201 CreatedLocation: /articles/63636

As soon as you know the new resource location, you can use PUT again to do updates to the blue stapler article. But as said before: you CAN add new resources through PUT as well. The next example is perfectly valid if your API provides this functionality:

PUT /articles/green-stapler HTTP/1.1
green stapler
9.95
HTTP/1.1 201 CreatedLocation: /articles/green-stapler

Here, the client decided on the actual resource URL.

Caveats

  • PUT and POST are both unsafe methods. However, PUT is idempotent, while POST is not.
- See more at: http://restcookbook.com/HTTP%20Methods/put-vs-post/#sthash.jQ4qPExo.dpuf

转载地址:http://qrfpf.baihongyu.com/

你可能感兴趣的文章
python多线程_thread与threading(推荐使用)
查看>>
pip安装openpyxl失败,更换镜像源
查看>>
selenium错误提示: ElementNotVisibleException: Message: element not interactable
查看>>
python 使用ddt数据驱动
查看>>
selenium之 如何控制网页内嵌div中滚动条的滚动
查看>>
【经验分享】XPATH逻辑运算
查看>>
selenium is not clickable at point (122, 347). Other element would receive the click
查看>>
python+selenium 浏览器无界面模式运行
查看>>
appium使用过程中的踩坑集
查看>>
appium的XPATH获取text值的方式与selenium区别
查看>>
处理appium获取toast内容
查看>>
解决uiautomatorviewer中添加xpath的方法
查看>>
Windows Server R2 安装python时报策略不允许的解决方案
查看>>
pip无法安装:换成国内镜像
查看>>
jenkins安装提示Please wait while Jenkins is getting ready to work...(Jenkins访问资源慢的问题)
查看>>
python安装mysqlclient[MySQLdb]
查看>>
性能测试的必要性评估以及评估方法
查看>>
性能测试需求分析
查看>>
性能测试需求评审
查看>>
性能测试实施流程
查看>>