[PR #11290/16703bb9 backport][3.13] Fix file uploads failing with HTTP 422 on 307/308 redirects #11297
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a backport of PR #11290 as merged into master (16703bb).
What do these changes do?
These changes fix a bug where file uploads fail with HTTP 422 errors when encountering 307/308 redirects. The issue occurs because:
The fix includes:
Are there changes in behavior for the user?
File uploads that encounter 307/308 redirects will now work correctly instead of failing with HTTP 422 errors. This is a bug fix that restores expected behavior - no breaking changes.
Is it a substantial burden for the maintainers to support this?
No. The changes are minimal and focused:
Related issue number
Fixes #11270
Implementation Details
Changes Made
aiohttp/client.py
data = req.data
todata = req._body
to avoid payload recreationaiohttp/payload.py
size
property to store start position on first accesstests/test_payload.py
tests/test_client_functional.py
Technical Explanation
The root cause identified by Cycloctane in the issue: "Two requests share the same BufferedReaderPayload instance, while the second request get zero Content-Length."
This happens because:
file_size - current_position
, which equals 0 at EOFThe fix stores the initial file position when size is first accessed, then always calculates size from that position. This ensures consistent Content-Length across multiple uses of the same payload instance.
Checklist
CONTRIBUTORS.txt
11270.bugfix.rst
into theCHANGES/
folder