2
votes

I am trying to send bytes as a header value over aiohttp:

payload = {
#ommited for brevity
}

encoded_payload = str.encode(json.dumps(payload))
b64 = base64.b64encode(encoded_payload)

# sign the requests
signature = hmac.new(str.encode(keys['private']), b64, hashlib.sha384).hexdigest()

headers = {
        'Content-Type': 'text/plain',
        'APIKEY': keys['public'],
        'PAYLOAD': b64, // base64 value
        'SIGNATURE': signature
    }

async with aiohttp.request(method="POST", url="example.com", headers=headers) as response:
    print(await response.text())

However, I receive an error:

Traceback (most recent call last): File "get_gem.py", line 34, in loop.run_until_complete(get_gemini()) File "/home/thorad/anaconda3/lib/python3.6/asyncio/base_events.py", line 466, in run_until_complete return future.result() File "get_gem.py", line 29, in get_gemini async with aiohttp.request(method="POST", url=base_url + payload["request"], headers=headers) as response: File "/home/thorad/anaconda3/lib/python3.6/site-packages/aiohttp/client.py", line 692, in aenter self._resp = yield from self._coro File "/home/thorad/anaconda3/lib/python3.6/site-packages/aiohttp/client.py", line 277, in _request resp = req.send(conn) File "/home/thorad/anaconda3/lib/python3.6/site-packages/aiohttp/client_reqrep.py", line 463, in send writer.write_headers(status_line, self.headers) File "/home/thorad/anaconda3/lib/python3.6/site-packages/aiohttp/http_writer.py", line 247, in write_headers [k + SEP + v + END for k, v in headers.items()]) File "/home/thorad/anaconda3/lib/python3.6/site-packages/aiohttp/http_writer.py", line 247, in [k + SEP + v + END for k, v in headers.items()]) TypeError: must be str, not bytes

which suggests that I can't send bytes as a header.

Unfortunately, the service that I am using requires me to do so, or else it returns an error.

  • I have tried removing 'Content-Type': 'text/plain'

How can I send bytes as a header through aiohttp? Thanks for your help.

2

2 Answers

4
votes

The issue here is that b64encode returns bytes, but those can be easily converted to a proper unicode string. It won't make a difference with your server.

>>> b64 = base64.b64encode(b'...')
>>> type(b64)
<class 'bytes'>
>>> b64 = base64.b64encode(b'...').decode('utf8')
>>> type(b64)
<class 'str'>
0
votes

Convert the payload in ascii:

payload = {
#ommited for brevity
}

encoded_payload = str.encode(json.dumps(payload))
b64 = base64.b64encode(encoded_payload)
b64 = b4.decode('ascii')  # conversion back to unicode

# sign the requests
signature = hmac.new(str.encode(keys['private']), b64, hashlib.sha384).hexdigest()

headers = {
        'Content-Type': 'text/plain',
        'APIKEY': keys['public'],
        'PAYLOAD': b64, // base64 value
        'SIGNATURE': signature
    }

async with aiohttp.request(method="POST", url="example.com", headers=headers) as response:
    print(await response.text())