Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raising incorrect error when passing array to data #3471

Open
AS7RIDENIED opened this issue Jan 9, 2025 · 2 comments
Open

Raising incorrect error when passing array to data #3471

AS7RIDENIED opened this issue Jan 9, 2025 · 2 comments

Comments

@AS7RIDENIED
Copy link

httpx Version: 0.28.1

import asyncio, httpx

async def test():
    sync_client = httpx.Client(base_url="https://httpbin.org")
    async_client = httpx.AsyncClient(base_url="https://httpbin.org")

    try:
        response = await async_client.request("POST", "/post", data=[{"a": "b"}])
        print(response)
    except Exception as e:
        print(e)

    try:    
        response = sync_client.request("POST", "/post", data=[{"a": "b"}])
        print(response)
    except Exception as e:
        print(e)


asyncio.run(test())
Attempted to send an sync request with an AsyncClient instance.
sequence item 1: expected a bytes-like object, dict found

In data, judging by the code sources, you can only pass strings, dictionaries, or bytes (+= arrays of bytes). If you pass any array other than a byte array to data, it will raise an incorrect error: Attempted to send a sync request with an AsyncClient instance. (We're using an asynchronous client to send an asynchronous request, mhm)
Honestly, I'm too lazy to look through the sources and figure out what went wrong, but in general, it should return the same error as it does with a synchronous client - sequence item 1: expected a bytes-like object, dict found.

@bagowix
Copy link

bagowix commented Jan 22, 2025

Hi!
I have not been able to replicate this error, it currently returns “Use ‘content=<...>’ to upload raw bytes/text content.”

@AS7RIDENIED
Copy link
Author

I just created a venv, installed httpx, and got the same exceptions as mentioned above. Also tried with python 3.12.8 but got same result

There is some data and traceback

Win11 24h2
(.venv) PS D:\Google Drive Files\Projects\!CurrentWork\httpx.test> pip freeze

anyio==4.8.0
certifi==2024.12.14
h11==0.14.0
httpcore==1.0.7
httpx==0.28.1
idna==3.10
sniffio==1.3.1
typing_extensions==4.12.2

(.venv) PS D:\Google Drive Files\Projects\!CurrentWork\httpx.test> python -V

Python 3.11.5

(.venv) PS D:\Google Drive Files\Projects\!CurrentWork\httpx.test> python .\test.py

Attempted to send an sync request with an AsyncClient instance.
sequence item 1: expected a bytes-like object, dict found

(.venv) PS D:\Google Drive Files\Projects\!CurrentWork\httpx.test> cat .\test.py

import asyncio, httpx

async def test():
    sync_client = httpx.Client(base_url="https://httpbin.org")
    async_client = httpx.AsyncClient(base_url="https://httpbin.org")

    try:
        response = await async_client.request("POST", "/post", data=[{"a": "b"}])
        print(response)
    except Exception as e:
        print(e)

    try:
        response = sync_client.request("POST", "/post", data=[{"a": "b"}])
        print(response)
    except Exception as e:
        print(e)


asyncio.run(test())

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants