请求模块可选标头参数不接入标题= ...语法
我正在编写一个简单的测试脚本,由于某种原因,调用相同的获取请求并使用相同的语法后,我得到了两个不同的结果。这是我在使用错误的语法(不使用标头= ...)的两个不同API时得到的:
>> import requests
>> head1 = { 'key' : 'API-key1' }
>> url1 = 'https://url1.com'
>> r1 = requests.get(url1, head1) ## Notice headers=head1 wasn't used
>> print(r1)
<Response [200]> ## Yet a valid response prints out
>> print(r1.json())
{'response': 'valid response'}
>> head2 = { 'key' : 'API-key2' }
>> url2 = 'https://url2.com'
>> r2 = requests.get(url2, head2) ## But in this instance
>> print(r2)
<Response [401]>
>> print(r2.json()) ## I get an 401 error
<bound method Response.json of <Response [401]>>
同样,如果我使用正确的语法,这就是我得到的,
>> import requests
>> head1 = { 'key' : 'API-key1' }
>> url1 = 'https://url1.com'
>> r1 = requests.get(url1, headers=head1) ## Using the correct syntax
>> print(r1)
<Response [200]>
>> print(r1.json()) ## But it received an error
{'response': {'error': [{'msg': 'No API access key supplied'}]}}
>> head2 = { 'key' : 'API-key2' }
>> url2 = 'https://url2.com'
>> r2 = requests.get(url2, headers=head2) ## This works
>> print(r2)
<Response [200]>
>> print(r2.json())
{'response': 'valid response'}
这就是我的API问题用于第一个请求?如何使用一个API的第一个api来获取标题不正确的语法,但是使用正确的语法,它可以成功地响应,但说它没有拿到我放在标题中的API键?但是,第二个API看起来正常工作,这令人困惑。
I'm writing a simple test script and for some reason, I get two different results after calling the same GET request and using the same syntax. This is what I get when using the wrong syntax (NOT using headers=...) for two different APIs:
>> import requests
>> head1 = { 'key' : 'API-key1' }
>> url1 = 'https://url1.com'
>> r1 = requests.get(url1, head1) ## Notice headers=head1 wasn't used
>> print(r1)
<Response [200]> ## Yet a valid response prints out
>> print(r1.json())
{'response': 'valid response'}
>> head2 = { 'key' : 'API-key2' }
>> url2 = 'https://url2.com'
>> r2 = requests.get(url2, head2) ## But in this instance
>> print(r2)
<Response [401]>
>> print(r2.json()) ## I get an 401 error
<bound method Response.json of <Response [401]>>
Similarly, if I use the correct syntax for both, this is what I get
>> import requests
>> head1 = { 'key' : 'API-key1' }
>> url1 = 'https://url1.com'
>> r1 = requests.get(url1, headers=head1) ## Using the correct syntax
>> print(r1)
<Response [200]>
>> print(r1.json()) ## But it received an error
{'response': {'error': [{'msg': 'No API access key supplied'}]}}
>> head2 = { 'key' : 'API-key2' }
>> url2 = 'https://url2.com'
>> r2 = requests.get(url2, headers=head2) ## This works
>> print(r2)
<Response [200]>
>> print(r2.json())
{'response': 'valid response'}
Is this an issue with the API I'm using for the first request? How come the first GET request with one API take in incorrect syntax for headers, but with correct syntax, it responds with a successful GET but says that it didn't take in the API key I put in the header? The second API looks like it works properly, though, which is confusing.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是 requests.gets.get 已定义:
注意一个必需的参数
url
,然后是一个关键字参数params
,然后是常规** *Kwarg
。因此,当您通过requests.get(url1,head1)
时,head1
作为params
kwarg,而不是headers
。这就是为什么当您通过明确说明名称来传递它时,它会起作用。请注意,第一种方式不是错误的语法,关键字,位置和必需的参数之间存在差异,并且有些重叠。此答案解释更好
Here is how requests.get is defined:
Notice how there's one required argument
url
, followed by one keyword argumentparams
and then the general**kwarg
. So when you passrequests.get(url1, head1)
, thehead1
is being passed as theparams
kwarg, not asheaders
. This is why when you pass it by explicitly stating the name, it works.Note that the first way isn't incorrect syntax, there is a difference between keyword, positional, and required arguments, with some overlap. This answer explains it better