[英]what is the difference between data and params in requests?
I am using python requests
module, I send my params like this before:我正在使用
python requests
模块,我之前发送我的参数是这样的:
requests.post(url=url, params=params)
but today, I find that I send my data like this, it fails, I change to this:但是今天,我发现我这样发送我的数据,它失败了,我改成这样:
requests.post(url=url, data=params)
then it is ok, what is the difference between data
and params
?那么没关系,
data
和params
有什么区别?
I observed that the request got a header X-Requested-With:XMLHttpRequest
, is it because of this?我观察到请求有一个标头
X-Requested-With:XMLHttpRequest
,是因为这个吗?
According to the requests documentation<\/a> :根据
请求文档<\/a>:
requests.post(url, data=data)<\/code> will make an
HTTP POST<\/a> request, and
requests.post(url, data=data)<\/code>将发出
![](/img/trans.png)
HTTP POST<\/a>请求,并且
<\/li>
A requests.get(url, params=params)<\/code> will make an
HTTP GET<\/a> request
requests.get(url, params=params)<\/code>将发出
![](/img/trans.png)
HTTP GET<\/a>请求
<\/li><\/ul>To understand the difference between the two, see this answer<\/a> .
要了解两者之间的区别,
请参阅此答案<\/a>。
Here's how params can be used in a GET:下面是如何在 GET 中使用参数:
payload = {'key1': 'value1', 'key2': 'value2'} r = requests.get('http:\/\/httpbin.org\/get', params=payload) print(r.text)<\/code><\/pre> Which outputs哪个输出
![](/img/trans.png)
![](/img/trans.png)
{ "args": { "key1": "value1", "key2": "value2" }, [...] "url": "http:\/\/httpbin.org\/get?key1=value1&key2=value2" }<\/code><\/pre>
![](/img/trans.png)
Notice that the payload ended up in the query string<\/a> of the URL.请注意,有效负载最终出现在 URL 的
查询字符串<\/a>中。 Since they ended up there, they are viewable by anyone who has access to the URL, which is why you shouldn't use query strings for sensitive data like passwords.
由于它们最终到达那里,任何有权访问该 URL 的人都可以查看它们,这就是为什么您不应该将查询字符串用于密码等敏感数据的原因。
Here's how data can be used in a POST:以下是如何在 POST 中使用数据:
payload = 'foobar' r = requests.post('http:\/\/httpbin.org\/post', data=payload) print(r.text)<\/code><\/pre> Which outputs哪个输出
![](/img/trans.png)
{ "args": {}, "data": "foobar", [...] "url": "http:\/\/httpbin.org\/post" }<\/code><\/pre> Notice how the POST data does not show up in the query strings, as they are transmitted through the body of the request instead.请注意 POST 数据如何没有显示在查询字符串中,因为它们是通过请求的主体传输的。
Critique of this answer has pointed out that there are more options.对此答案的批评指出,还有更多选择。 I never denied such a thing in my original answer, but let's take a closer look.
我在最初的答案中从未否认过这样的事情,但让我们仔细看看。
The documentation examples always show:文档示例始终显示:
The params keyword used for GET, and用于 GET 的 params 关键字,以及<\/li>
The data keyword used for POST用于 POST 的数据关键字<\/li><\/ul>But that doesn't mean they are mutually exclusive.
但这并不意味着它们是相互排斥的。
In theory you could mix the two together in a POST:理论上,您可以在 POST 中将两者混合在一起:
data = 'foobar' params = {'key1': 'value1', 'key2': 'value2'} r = requests.post('http:\/\/httpbin.org\/post', params=params, data=data) print(r.text)<\/code><\/pre> Which outputs哪个输出
![](/img/trans.png)
{ "args": { "key1": "value1", "key2": "value2" }, "data": "foobar", [...] "url": "http:\/\/httpbin.org\/post?key1=value1&key2=value2" }<\/code><\/pre> But you cannot mix data into a GET:但是您不能将数据混合到 GET 中:
data = 'foobar' params = {'key1': 'value1', 'key2': 'value2'} r = requests.get('http:\/\/httpbin.org\/get', params=params, data=data) print(r.text)<\/code><\/pre> Outputs:输出:
{ "args": { "key1": "value1", "key2": "value2" }, [...] "url": "http:\/\/httpbin.org\/get?key1=value1&key2=value2" }<\/code><\/pre> Notice how the data field is gone.注意数据字段是如何消失的。
"
First of all, there are two different methods<\/strong> :首先,有两种不同的方法<\/strong>:
requests.post()<\/code> makes a POST request (placing all the parameters in the body)
requests.post()<\/code>发出 POST 请求(将所有参数放在正文中)
<\/li>
requests.get()<\/code> makes a GET request (placing all the parameters in the URL)
requests.get()<\/code>发出 GET 请求(将所有参数放在 URL 中)
<\/li><\/ul> Then, according to the docs<\/a> , you can choose between two parameters<\/strong> to send all the key\/value data:
然后,
根据文档<\/a>,您可以在两个参数<\/strong>之间进行选择以发送所有键\/值数据:
-
params=<\/code> , without string modifications.
params=<\/code> ,没有字符串修改。
<\/li>
![](/img/trans.png)
data=<\/code> , applying a form-encoding string modification to the parameters.
data=<\/code> ,对参数应用表单编码字符串修改。
<\/li><\/ul> So, you have 4 choices to send your request:
因此,您有 4 个选择来发送请求:
-
requests.post(url, params=)<\/code><\/li>
![](/img/trans.png)
requests.post(url, data=)<\/code><\/li>
![](/img/trans.png)
requests.get(url, params=)<\/code><\/li>
![](/img/trans.png)
requests.get(url, data=)<\/code><\/li><\/ul> I don't think the currently accepted answer is correct.我不认为当前接受的答案是正确的。 He is actually talking about requests.post()<\/code> but using requests.get()<\/code> in his own example.
他实际上是在谈论requests.post()<\/code>但在他自己的示例中使用requests.get()<\/code> 。
"
Params are sent in (appended to) the URI ( http://www.answer.com/here?param1=1¶m2=2
) while data is sent in the request body .参数在(附加到)URI (
http://www.answer.com/here?param1=1¶m2=2
) 中发送,而数据在请求正文中发送。 Usually sensitive data or that sent in large volumes is posted in the body because it's easier to secure and doesn't lead to huge URIs.通常敏感数据或大量发送的数据会发布在正文中,因为它更容易保护并且不会导致巨大的 URI。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.