我试图用包含完整冒号的标题发出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'
,那么我做错了什么?
答案 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'})