下面是curl中的HTTPS调用
header1="projectName: zhikovapp"
header2="Authorization: Bearer HZCdsf="
bl_url="https://BlazerNpymh.com/api/documents?pdfDate=$today"
curl -s -k -H "$header1" -H "$header2" "$bl_url"
我想用requests模块编写一个等价的python调用。
header ={
"projectName": "zhikovapp",
"Authorization": "Bearer HZCdsf="
}
response = requests.get(bl_url, headers = header)
但是,请求无效。怎么了?
返回的响应的内容如下所示
<Response [400]>
_content = '{"Message":"The request is invalid."}'
headers = {'Content-Length': '37', 'Access-Control-Allow-Headers': 'projectname, authorization, Content-Type', 'Expires': '-1', 'cacheControlHeader': 'max-age=604800', 'Connection': 'keep-alive', 'Pragma': 'no-cache', 'Cache-Control': 'no-cache', 'Date': 'Sat, 15 Oct 2016 02:41:13 GMT', 'Access-Control-Allow-Origin': '*', 'Access-Control-Allow-Methods': 'GET, POST, PUT, DELETE, OPTIONS', 'Content-Type': 'application/json; charset=utf-8'}
reason = 'Bad Request'
我正在使用Python2.7
编辑:Soviut指出一些syntex错误后,我进行了更正。
在} 参数应该定义为一个字典,一组键/值对。您已经定义了一组字符串(唯一列表)。
request.get()
中,^{你应该像这样声明你的头:
注意字典中每一行的
"key": "value"
格式。编辑:你的
Access-Control-Allow-Headers
说他们会接受projectname
和authorization
小写。您已经用大写字母命名了头projectName
和Authorization
。如果不匹配,就会被拒绝。curl
调用的shell中定义了$today
,并且没有在requests
的调用URL中替换它,那么这可能是导致400错误请求的原因。Access-Control-*
和其他CORS头与非浏览器客户端无关。此外,HTTP头通常不区分大小写。以下是@furas的建议:
与
requests
相同的请求:正如您所看到的,唯一的区别是
User-Agent
头。这不太可能是原因,但您可以很容易地在headers
中将其设置为您喜欢的值。相关问题 更多 >
编程相关推荐