aio-libs / aiohttp

Asynchronous HTTP client/server framework for asyncio and Python
https://docs.aiohttp.org
Other
15.15k stars 2.02k forks source link

Reading multipart form data with text fields #3619

Closed parthibd closed 3 months ago

parthibd commented 5 years ago

I was able to read a file using multipart reader. How do I extract text fields? I am lost. An example would be really helpful.

aio-libs-bot commented 5 years ago

GitMate.io thinks the contributor most likely able to help you is @asvetlov.

Possibly related issues are https://github.com/aio-libs/aiohttp/issues/3034 (Multipart file form data with name), https://github.com/aio-libs/aiohttp/issues/903 (How to pass array to form data (multipart/form-data)), https://github.com/aio-libs/aiohttp/issues/304 (multipart tests), https://github.com/aio-libs/aiohttp/issues/880 (MultipartReader does not support multipart data with a preamble), and https://github.com/aio-libs/aiohttp/issues/490 (Don't work "multipart/form-data" file upload.).

kornicameister commented 5 years ago

When you're reading a file using multipart reader you most likely referenced that file by name. That's how multipart work AFAIR, you're labelling parts in entire multipart. The text fields ought have the names as well.

Given you used web_request.Request#post coroutine for that you ought to get the values of the fields from returned dictionary. If you're using web_request.Request#mutlipart you got to traverse all the parts manually looking for the one that has the name you're interested in the moment.

parthibd commented 5 years ago

Thanks a lot for the solution . I had to manually traverse all the parts to extract them out from the request . My problem is solved . :)

kornicameister commented 5 years ago

Just so you know, using post couroutine comes with the price. If there's a file sent you may face a larger memory usage as the file is being loaded behind the scenes for you. That's why when it comes to files you might want to use async for or next on the reader you obtained via multipart looking for fields manually. That's of course valid only if you have a chance to not read an entire file and for instance write portion of bytes in chunks to the server filesystem. Otherwise I guess there are other concepts like restricting the size of the file via content-length. Although, AFAIR, there's an entry in code or documentation that says that content-lenght might not be reliable when it comes to larger, perhaps only, files.

I'd assume that @asvetlov or someone else from core team might be able to shed more light here, if you're interested of course :)

parthibd commented 5 years ago

Thanks for the heads up. :) I did notice sluginess when I was looking through the fields containing larger files. I'm using async for for this purpose.