我试图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:Bearer': 'somelongstring', # see this line
'Accept-Language': 'en_US',
'Accept-Encoding': 'gzip',
'Content-Type': 'application/x-www-form-urlencoded',
'Connection': 'close'}
既Authorization:bearer
不能也
'Authorization\\': 'bearer: somelongstring'
不能工作,我需要它被接收
'Authorization:Bearer': 'somelongstring'
,所以我做错了什么?