Search code examples
tornadopython-3.3

how to encode header with `:` in tornadoweb http request object


im trying to make a POST request with a header that contains a full colon, how would i create the object to get this to work? here are my attempts:

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

when posted, it produces in the request headers:

{'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'}

and when i try this

    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'})

this on the otherhand produces:

{'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'}

neither Authorization&#58bearer nor 'Authorization\\': 'bearer: somelongstring' can work, i need it to be received as 'Authorization:Bearer': 'somelongstring', so what i'm i doing wrong?


Solution

  • The challenge is that you are trying to add an invalid header name. What you are probably referring to is the Authorize header, with a value of Bearer:longstring. So your first sample becomes:

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