sonic182 / aiosonic

A very fast Python asyncio http client
https://aiosonic.readthedocs.io/en/latest/
MIT License
153 stars 19 forks source link

multipart/form-data #493

Closed luwqz1 closed 1 week ago

luwqz1 commented 2 months ago

Is your feature request related to a problem? Please describe. No

Describe the solution you'd like I would like to have class Formdata with which it would be possible to create multipart/form-data and send files

Describe alternatives you've considered Formdata is in aiohttp source code

sonic182 commented 2 months ago

Have you tried?


    # ##################
    # Post data as multipart form
    # ##################
    url = "https://postman-echo.com/post"
    posted_data = {'foo': 'bar'}
    response = await client.post(url, data=posted_data)
luwqz1 commented 2 months ago

Have you tried?

    # ##################
    # Post data as multipart form
    # ##################
    url = "https://postman-echo.com/post"
    posted_data = {'foo': 'bar'}
    response = await client.post(url, data=posted_data)

Sorry i did not specify what i need... How do i send files using multipart/form-data? For example, requests library have parameters data and files.

import pathlib

import requests

kitten_bytes = pathlib.Path("kitten.jpg").read_bytes()
requests.post("https://kitten.com", files={"kitten.jpg": kitten_bytes})

I think parameter files will be even better than the Formdataclass from aiohttp, what do you think? or maybe there are some other solutions?

sonic182 commented 2 weeks ago

Seems reasonable, to have a intermediate class that will hold the references to form fields, and then, to stream the form data efficiently

luwqz1 commented 2 weeks ago

Seems reasonable, to have a intermediate class that will hold the references to form fields, and then, to stream the form data efficiently

Can you please provide an example?

sonic182 commented 2 weeks ago

Seems reasonable, to have a intermediate class that will hold the references to form fields, and then, to stream the form data efficiently

Can you please provide an example?

Sorry for the delay

Seeing my code, seems that I already did it.

@pytest.mark.asyncio
async def test_post_multipart_to_django(live_server):
    """Test post multipart."""
    url = live_server.url + "/post_file"
    data = {"foo": open("tests/files/bar.txt", "rb"), "field1": "foo"}

    async with aiosonic.HTTPClient() as client:
        res = await client.post(url, data=data, multipart=True)
        assert res.status_code == 200
        assert await res.text() == "bar-foo"

basically using the open() method with binary reading.

Make sure your filename has the correct extension. The server will receive as filename, the os.path.basename(<path>) of the path that open(<path>) received, in this example "bar.txt"

sonic182 commented 1 week ago

New MultipartForm class https://aiosonic.readthedocs.io/en/latest/reference.html#multipart-form-data

luwqz1 commented 1 week ago

New MultipartForm class https://aiosonic.readthedocs.io/en/latest/reference.html#multipart-form-data

Thanks for the work and feedback!