aio-libs / aio-libs/aiohttp

Manually give content-length of FormData fields

オープン
#7,174 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る
enhancement
主要言語
Python
スター
16.5k
フォーク
2.4k
平均マージ
20時間 10分
マージ済み PR(30日)
221

説明

### Is your feature request related to a problem?

Oh, the terrors of the internet.

The backend I am working with requires _Content-Length_ to be specified for the whole multipart request, which is discussed in the documentation here:

https://docs.aiohttp.org/en/stable/multipart.html#hacking-multipart

However, it seems like it is only possible to figure out what _Content-Length_ is by buffering the data to be sent.

In my situation, sometimes I have a large file on disk with a known size beforehand which I would like to include in a multi-part post request. The documentation also discusses "streaming uploads:"

https://docs.aiohttp.org/en/stable/client_quickstart.html#streaming-uploads

It would be nice if I could combine these solutions:

1. multi-part post request
2. _Content-Length_ for the whole request
3. streaming upload

Currently, (1) and (2) can be achieved using `aiohttp.FormData` with file-like objects, but using `io.IOBase` should be avoided because we are calling blocking IO inside an async context.

### Describe the solution you'd like

`aiohttp.FormData.add_field` should be extended in two ways:

1. support being given `AsyncIterable[bytes]` as value
2. accept optional parameter `content_length` which, if given, manually sets the length of the part.

When trying to send the `FormData`: if the lengths of all parts are known, then set the header *Content-Length*.

### Describe alternatives you've considered

Currently I am using `aiohttp.FormData` with file-like objects and accepting the performance penalty. If the files get too large, I will write a custom multi-part deserializer, hard-coding its parts into a string template and calculating the Content-Length manually. The hard-coded parts plus file "stream" would be wrapped similarly to how it's done here: https://github.com/aio-libs/aiohttp/issues/3598#issuecomment-463630698

### Related component

Client

### Additional context

_No response_

### Code of Conduct

- [X] I agree to follow the aio-libs Code of Conduct

コントリビューションガイド

コントリビューションガイドを開く

評価

この issue はまだ評価されていません。

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。