Skip to content

aiohttp converts data to string #781

@bixb922

Description

@bixb922

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"
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions