为什么指定JSON并在指定数据时python请求。
我正在使用请求
库发送http请求并找到以下行为奇怪:当我使用json =
来构建请求的身体时,其类型变为bytes
,当我使用data =
构建请求的主体时,其类型保留str
。是否有原因为什么使用JSON =
会引导请求
库开发人员对身体右边的范围进行编码?
In [8]: requests.Request('GET', 'https://httpbin.org/get', json={'a': 5}).prepare().body
Out[8]: b'{"a": 5}' # bytes
In [9]: requests.Request('GET', 'https://httpbin.org/get', data=json.dumps({'a': 5})).prepare().body
Out[9]: '{"a": 5}' # str
来自requests.models
:
def prepare_body(self, data, files, json=None):
"""Prepares the given HTTP body data."""
# Check if file, fo, generator, iterator.
# If not, run through normal process.
# Nottin' on you.
body = None
content_type = None
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
# provides this natively, but Python 3 gives a Unicode string.
content_type = 'application/json'
try:
body = complexjson.dumps(json, allow_nan=False)
except ValueError as ve:
raise InvalidJSONError(ve, request=self)
if not isinstance(body, bytes):
body = body.encode('utf-8') # !!! ACTIVELY ENCODED HERE !!!
I am using requests
library to send HTTP requests and found the following behavior bizarre: when I use json=
to build request's body, its type becomes bytes
, and when I use data=
to build request's body, its type remains str
. Is there a reason why using json=
would lead requests
library developers to encode the body right-away?
In [8]: requests.Request('GET', 'https://httpbin.org/get', json={'a': 5}).prepare().body
Out[8]: b'{"a": 5}' # bytes
In [9]: requests.Request('GET', 'https://httpbin.org/get', data=json.dumps({'a': 5})).prepare().body
Out[9]: '{"a": 5}' # str
From requests.models
:
def prepare_body(self, data, files, json=None):
"""Prepares the given HTTP body data."""
# Check if file, fo, generator, iterator.
# If not, run through normal process.
# Nottin' on you.
body = None
content_type = None
if not data and json is not None:
# urllib3 requires a bytes-like body. Python 2's json.dumps
# provides this natively, but Python 3 gives a Unicode string.
content_type = 'application/json'
try:
body = complexjson.dumps(json, allow_nan=False)
except ValueError as ve:
raise InvalidJSONError(ve, request=self)
if not isinstance(body, bytes):
body = body.encode('utf-8') # !!! ACTIVELY ENCODED HERE !!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试此代码以行使许多可能的请求配置,并注意与每个结果准备好的Quect创建的车身类型和标头的差异:
要注意的一件事是,如果您同时填充数据和JSON参数参数,请求请求默认情况下使用数据并忽略JSON 。
我没有调查为身体提供流。
在库的源文件中查看prepar_body,以查看这是如何发生的:
https://github.com/psf/requests/blob/88dce9d854797c05d0ff296b70e0430535353535355353553555555555.Src/src/requests/requests/models.py#l494
如果我也开始探索发送功能,以查看我的事件在发送请求之前,但我还没有弄清楚那部分
try this code to exercise a number of possible Request configurations and note the differences in the body type and headers created with each resulting PreparedRequest:
One thing to note is that if you populate both data and json parameters requests will use data by default and ignore json.
I did not investigate providing a stream for the body.
Look for prepare_body in the library's source file to see how this happens:
https://github.com/psf/requests/blob/88dce9d854797c05d0ff296b70e0430535ef8aaf/src/requests/models.py#L494
i also started looking into the send function to see if i could figure out what further conversions occur before the request is sent, but i have not figured that part out yet