简体   繁体   English

为什么当GET也可以充当POST时,为什么需要在RestFul中使用GET和POST方法

[英]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的请求:

  • GET to retrieve data GET检索数据
  • POST to update a record POST更新记录
  • PUT to insert a record 插入记录
  • DELETE to delete a record 删除以删除记录

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.

相关问题 RESTful Web服务-getRemoteUser使用GET,但不能使用POST! 为什么? - RESTful Webservice - getRemoteUser works with GET but not with POST! Why? RestFul获取与发布 - RestFul Get vs Post 当我们要使用Spring @ModelAttribute时,我们是否需要为http get和post使用类似的@RequestMapping值? - Do we need similar @RequestMapping value for both http get and post when we are going to use Spring @ModelAttribute 将Json发送到RestFul Web服务的HTTP POST方法时,如何获取Bean对象? - How to get Bean object when Json is sent to a HTTP POST method of RestFul web service? 如何使用发布方式而不是实现宁静的下载服务 - How to use post method instead of get to realize restful downloading service 为什么我们在mockmvc中测试POST方法时需要将请求体设置为UTF-8字符串 - Why do we need to set the request body as a UTF-8 string when testing the POST method in mockmvc 为什么 OkHttp post 方法发送 get 请求而不是 post 请求? - Why is it OkHttp post method sends get request instead of post request? 为什么我们可以在Restful服务中使用jsonObject时需要POJO - Why we need POJO when we can use jsonObject in Restful services RESTful服务-GET和POST请求不起作用 - RESTful Services - GET and POST requests not working RESTEasy - 在@ GET / @ POST时重用方法参数 - RESTEasy - Reuse method parameters when @GET/@POST
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM