如何在tornadoweb http请求对象中用`:`编码头

时间:2013-11-14 16:58:13

标签: tornado python-3.3

我试图用包含完整冒号的标题发出POST请求,我将如何创建对象以使其工作? 这是我的尝试:

    req = HTTPRequest(
          "http://myapp:8080/debug",
          method='POST', headers={'Accept': 'application/json',
                       "Accept-Language": "en_US",
                       'Authorization:Bearer': 'somelongstring'},
                       body= {'fancy':'dict'})

发布时,会在请求标题中生成:

{'Accept': 'application/json',
 'Authorization\\': 'bearer: somelongstring',   # this is the line  
 'Content-Length': '276', 
 'Host': 'myapp:8080', 
 'Accept-Language': 'en_US', 
 'Accept-Encoding': 'gzip', 
 'Content-Type': 'application/x-www-form-urlencoded', 
 'Connection': 'close'}

当我尝试这个

    from urllib import parse
    auth_h = parse.quote('Authorization:Bearer')
    req = HTTPRequest(
          "http://myapp:8080/debug",
          method='POST', headers={'Accept': 'application/json',
                       "Accept-Language": "en_US",
                       auth_h: 'somelongstring'},
                       body= {'fancy':'dict'})

另一方面产生:

{'Accept': 'application/json', 
 'Host': 'myapp:8080', 
 'Content-Length': '276', 
 'Authorization&#58Bearer': 'somelongstring',     # see this line
 'Accept-Language': 'en_US', 
 'Accept-Encoding': 'gzip',
 'Content-Type': 'application/x-www-form-urlencoded',
 'Connection': 'close'}

既不Authorization&#58bearer也不 'Authorization\\': 'bearer: somelongstring'可以工作,我需要它作为 'Authorization:Bearer': 'somelongstring',那么我做错了什么?

1 个答案:

答案 0 :(得分:2)

您面临的挑战是尝试添加无效的标题名称。您可能指的是Authorize标头,其值为Bearer:longstring。所以你的第一个样本变为:

req = HTTPRequest(
          "http://myapp:8080/debug",
          method='POST', headers={'Accept': 'application/json',
                       "Accept-Language": "en_US",
                       'Authorization': 'Bearer:somelongstring'},
                       body= {'fancy':'dict'})