micropython / micropython-lib

Core Python libraries ported to MicroPython

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

aiohttp converts data to string

bixb922 opened this issue · comments

Nice to have aiohttp, since it supports the https protocol with 1.22!! I did not see aiohttp in the 1.22 announcement, it really is useful to have async https now!

When posting data with the data= option, the data gets converted to string before sending. This encloses the data in b'' before sending. For example data=b'hello' will send str(b'hello'), i.e. "b'hello'"

What is expected: I expected data= to accept bytes or bytearray, just like urequests or CPython aiohttp, and to transmit the raw bytes without converting nor encoding/decoding.

I am using: MicroPython v1.22.0 on 2023-12-27; Generic ESP32S3 module with Octal-SPIRAM with ESP32S3

Sample code (based on aiohttp examples):

import aiohttp
import asyncio
async def main():
    do_connect()
    async with aiohttp.ClientSession("http://httpbin.org") as session:
        async with session.post("/post",data=b'hello world') as resp:
            assert resp.status == 200
            rpost = await resp.text()
            print(f"POST: {rpost}")

asyncio.run(main())

The output is

POST: {
  "args": {}, 
  "data": "b'hello world'", 
  "files": {}, 
  "form": {}, 
  "headers": {
    "Content-Length": "14", 
    "Host": "httpbin.org", 
    "User-Agent": "compat", 
    "X-Amzn-Trace-Id": "Root=1-6594aa03-4f076b3a3480ef2659dfc035"
  }, 
  "json": null, 
  "origin": "181.43.38.11", 
  "url": "http://httpbin.org/post"
}

@bixb922 I think I may have overlooked binary data treatment, let me know if this #782 fixes it 👍🏼

Thank you very much for your fast response. Yes, this now works for me, transmitting binary data!!