在Python的requests中包含多个头部信息

14

我有下面这个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'

我正在使用Python 2.7

编辑:在Soviut指出一些语法错误后,我进行了更正。


你得到了什么结果?我猜你得到了一个语法错误。 - Soviut
2
尝试使用curl命令和您的代码访问URL http://httpbin.org/get,它将返回所有的headers,然后您可以比较两个版本中的headers。 - furas
你正在使用的URL是否公开? - Padraic Cunningham
2个回答

25
request.get() 中,headers 参数应该被定义为一个字典,也就是一组键值对。而你却定义了一个由字符串唯一构成的列表(即集合)。
你应该按照以下方式声明 headers:
headers = {
    "projectName": "zhikovapp",
    "Authorization": "Bearer HZCdsf="
}
response = requests.get(bl_url, headers=headers)

请注意字典中每行的"key": "value"格式。

编辑:您的Access-Control-Allow-Headers 表示它们将接受小写的projectnameauthorization。您的标题中使用了大写字母的projectNameAuthorization。如果它们不匹配,请求将被拒绝。


1
谢谢。我尝试过了,但仍然出现相同的错误。 - guagay_wk
什么错误?请在您的原始问题中发布它。 - Soviut
谢谢。我已经在问题上发布并根据您指出的错误更正了语法错误。 - guagay_wk
我已经更新了我的答案,以包含更多关于你的 Access-Control-Allow-Headers 错误的细节。 - Soviut

2
  1. If you have $today defined in the shell you make curl call from, and you don't substitute it in the requests' call URL, then it's a likely reason for the 400 Bad Request.
  2. Access-Control-* and other CORS headers have nothing to do with non-browser clients. Also HTTP headers are generally case insensitive.
  3. Following @furas's advice here's the output:

    $ curl -H "projectName: zhikovapp" -H "Authorization: Bearer HZCdsf=" \
        http://httpbin.org/get
    
    {
       "args": {}, 
       "headers": {
          "Accept": "*/*", 
          "Authorization": "Bearer HZCdsf=", 
          "Host": "httpbin.org", 
          "Projectname": "zhikovapp", 
          "User-Agent": "curl/7.35.0"
       }, 
       "origin": "1.2.3.4", 
       "url": "http://httpbin.org/get"
    }
    

    And the same request with requests:

    import requests
    res = requests.get('http://httpbin.org/get', headers={
      "projectName"   : "zhikovapp",
      "Authorization" : "Bearer HZCdsf="
    })
    print(res.json())
    
    {
      'args': {},
      'headers': {
         'Accept': '*/*',
         'Accept-Encoding': 'gzip, deflate, compress',
         'Authorization': 'Bearer HZCdsf=',
         'Host': 'httpbin.org',
         'Projectname': 'zhikovapp',
         'User-Agent': 'python-requests/2.2.1 CPython/3.4.3 '
           'Linux/3.16.0-38-generic'
       },
       'origin': '1.2.3.4',
       'url': 'http://httpbin.org/get'
    }
    

    As you can see the only difference is User-Agent header. It's unlikely the cause but you can easily set it in headers to the value you like.


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接