Closed luwqz1 closed 1 week 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)
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 Formdata
class from aiohttp
, what do you think? or maybe there are some other solutions?
Seems reasonable, to have a intermediate class that will hold the references to form fields, and then, to stream the form data efficiently
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?
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"
New MultipartForm class https://aiosonic.readthedocs.io/en/latest/reference.html#multipart-form-data
New MultipartForm class https://aiosonic.readthedocs.io/en/latest/reference.html#multipart-form-data
Thanks for the work and feedback!
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