[英]Why we need GET and POST method in RestFul when GET can behave as POST also
I know it a basic question but i was not able to find a answer. 我知道这是一个基本问题,但我找不到答案。 My question is why we need POST method in restful when we can even insert data from GET method also.
我的问题是,即使我们甚至可以从GET方法插入数据,为什么我们还是需要POST方法。 If there any specific functionality which make POST different from GET.
是否有使POST与GET不同的特定功能。
Thanks. 谢谢。
It is a good practice to use the standard methods offered by the HTTP protocol to handle the requests for a web service Restful: 优良作法是使用HTTP协议提供的标准方法来处理对Web服务Restful的请求:
Following this convention is easy for a person to understand a library that he doesn't know. 遵循此约定对一个人来说很容易理解他不知道的图书馆。
Just to have an idea if I need to get all companies 只是想知道是否需要让所有公司
GET /companies
To retrieve a particular company identifiable by 1 检索由1标识的特定公司
GET /companies/1
To create a new company: 创建新公司:
PUT /companies
TO update the company identifiable by 1 用1更新可识别的公司
POST /companies/1
To delete the company identified by 1 删除由1标识的公司
DELETE /companies/1
And extending this concept, to retrieve all the dependents of the company 1 并扩展此概念,以检索公司的所有家属1
GET /companies/1/dependents
To retrieve all the invoices of a company 检索公司的所有发票
GET /companies/1/invoices
and so on. 等等。
As you can see if you know what you like to do is easy to recreate all the urls to get, modify, create, delete data. 如您所见,您是否知道自己想做的事很容易重新创建所有URL以获取,修改,创建,删除数据。 It is not necessary to follow this convention, but it is a good idea specially if you are creating a web service usable from outside your company where it is important to define a standard for all.
不必遵循此约定,但是,如果要创建可从公司外部使用的Web服务(对于所有人定义一个标准很重要),则特别好是一个好主意。
Additionally, GET methods can be cached, and it is easy for existing infrastructure (proxies, firewalls) to do that. 此外,可以缓存GET方法,而现有基础架构(代理,防火墙)很容易做到这一点。
They are different methods and have different purpose and specification. 它们是不同的方法,具有不同的目的和规格。
Some other notes on GET requests:
有关GET请求的其他注意事项:
- GET requests can be cached
可以缓存GET请求
- GET requests remain in the browser history
GET请求保留在浏览器历史记录中
- GET requests can be bookmarked
GET请求可以加书签
- GET requests should never be used when dealing with sensitive data
处理敏感数据时,切勿使用GET请求
- GET requests have length restrictions
GET请求有长度限制
- GET requests should be used only to retrieve data
GET请求应仅用于检索数据
Some other notes on POST requests:
有关POST请求的其他注意事项:
- POST requests are never cached
POST请求永远不会被缓存
- POST requests do not remain in the browser history
POST请求不会保留在浏览器历史记录中
- POST requests cannot be bookmarked
POST请求无法添加书签
- POST requests have no restrictions on data length
POST请求对数据长度没有限制
They should be used appropriately. 应该适当地使用它们。 For more info on use and specification, look here .
有关使用和规格的更多信息,请参见此处 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.