When should we use PUT and when should we use POST?
来源:互联网 发布:钢琴软件带有键盘乐谱 编辑:程序博客网 时间:2024/04/28 10:45
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<article> <title>red stapler</title> <price currency="eur">12.50</price></article>
POST /articles HTTP/1.1<article> <title>blue stapler</title> <price currency="eur">7.50</price></article>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<article> <title>green stapler</title> <price currency="eur">9.95</price></article>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.
- When should we use PUT and when should we use POST?
- should we use annotations?
- NoSQL Databases, why we should use, and which one we should choose
- When Should You Use Templates
- When should I use shutdown()?
- When Should I Use HBase
- When should I use == and when should I use Equals?
- What should we do when we find an issue
- Why we should use closures in python
- When and Why do we use "#if 0"
- When and how should I use a ThreadLocal variable?
- when can we use machine learning ?
- When should I use Thread.getContextClassLoader()?
- FAQ When should I use refreshLocal?
- When you should use base64 for images
- What should we do when meet a crash in android?
- What should we do when meet a crash in android
- why should we use requset_mem_region()before using ioremap()?
- 随机获取数据库表记录行
- 如何拆分pdf文件的软件下载
- py in
- ORAchk-数据库健康检查好帮手
- mediaPlayer的seekto方法
- When should we use PUT and when should we use POST?
- 设计模式六大原则(3):依赖倒置原则
- php中实现精确设置session过期时间的方法
- android annotation 注入 @afterview 执行时间
- 过多if-else分支的优化
- PostgreSQL 相似字符串函数和操作符对比
- 【Android】 Android中spinner下拉列表的使用
- 做微信电商
- Memcached 集群架构方面的问题