发送带json body的get请求

 

用curl:

curl -X GET "127.0.0.1:8001" -d "{'a':1,'b':2}"

用python requests:

import requests
requests.get(url="127.0.0.1:8001", data={'a':1,'b':2})

参考:https://juejin.im/entry/5badb020f265da0ac962abf2

当我们被问及 HTTP 的 GET 与 POST 两种请求方式的区别的时候,很多答案是说 GET 的数据须通过 URL 以 Query Parameter 来传送 ,而 POST 可以通过请求体来发送数据,所以因 URL 的受限,往往 GET 无法发送太多的字符。

GET 果真不能通过 Request Body 来传送数据吗?

非也。

如此想法多半是因循着网页中 form 的 method 属性只有 get 与 post 两种而来。

因为把 form 的 method 设置为 post,表单数据会放在 body 中,而 method 为 get(默认值) 时,提交时浏览器会把表单中的字符拼接到 action 的 URL 后作为 query parameter 传送。

扫描二维码关注公众号,回复: 8459977 查看本文章

GET vs POST

于是乎就有了这么一种假像:HTTP GET 必须通过 URL 的查询参数来发送数据。

其实 HTTP 规范并未规定说 GET 就不能发送 body 数据,在 RFC GET 中只是说:

The GET method means retrieve whatever information (in the form of an entity) is identified by the Request-URI.

猜你喜欢

转载自www.cnblogs.com/zealousness/p/12166591.html