Skip to content

capture request data with aiohttp multipart/form-data #32543

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sdk/core/azure-core/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

### Bugs Fixed

- Fixed an issue with `multipart/form-data` in the async transport where `data` was not getting encoded into the request body. #32473

### Other Changes

## 1.29.4 (2023-09-07)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ def _get_request_data(self, request):
:return: The request data
"""
if request.files:
form_data = aiohttp.FormData()
form_data = aiohttp.FormData(request.data or {})
for form_file, data in request.files.items():
content_type = data[2] if len(data) > 2 else None
try:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,14 +221,25 @@ async def test_urlencoded_content(send_request):
)


# @pytest.mark.asyncio
# async def test_multipart_files_content(send_request):
# request = HttpRequest(
# "POST",
# "/multipart/basic",
# files={"fileContent": io.BytesIO(b"<file content>")},
# )
# await send_request(request)
@pytest.mark.asyncio
async def test_multipart_files_content(send_request):
request = HttpRequest(
"POST",
"/multipart/basic",
files={"fileContent": io.BytesIO(b"<file content>")},
)
await send_request(request)


@pytest.mark.asyncio
async def test_multipart_data_and_files_content(send_request):
request = HttpRequest(
"POST",
"/multipart/data-and-files",
data={"message": "Hello, world!"},
files={"fileContent": io.BytesIO(b"<file content>")},
)
await send_request(request)


@pytest.mark.asyncio
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def basic():
assert_with_message("content type", multipart_header_start, request.content_type[: len(multipart_header_start)])
if request.files:
# aiohttp
assert_with_message("content length", 258, request.content_length)
assert_with_message("content length", 228, request.content_length)
assert_with_message("num files", 1, len(request.files))
assert_with_message("has file named fileContent", True, bool(request.files.get("fileContent")))
file_content = request.files["fileContent"]
Expand All @@ -38,7 +38,7 @@ def basic():
)
assert_with_message(
"content disposition",
'form-data; name="fileContent"; filename="fileContent"; filename*=utf-8\'\'fileContent',
'form-data; name="fileContent"; filename="fileContent"',
file_content.headers["Content-Disposition"],
)
elif request.form:
Expand All @@ -52,9 +52,30 @@ def basic():

@multipart_api.route("/data-and-files", methods=["POST"])
def data_and_files():
assert_with_message("content type", multipart_header_start, request.content_type[: len(multipart_header_start)])
assert_with_message("message", "Hello, world!", request.form["message"])
assert_with_message("message", "<file content>", request.form["fileContent"])
if request.files:
# aiohttp
assert_with_message("content type", multipart_header_start, request.content_type[: len(multipart_header_start)])
assert_with_message("has file named fileContent", True, bool(request.files.get("fileContent")))
assert_with_message("message", "Hello, world!", request.form["message"])
file_content = request.files["fileContent"]
assert_with_message("file content type", "application/octet-stream", file_content.content_type)
assert_with_message("file content length", 14, file_content.content_length)
assert_with_message("filename", "fileContent", file_content.filename)
assert_with_message(
"has content disposition header", True, bool(file_content.headers.get("Content-Disposition"))
)
assert_with_message(
"content disposition",
'form-data; name="fileContent"; filename="fileContent"',
file_content.headers["Content-Disposition"],
)
elif request.form:
# requests
assert_with_message("content type", multipart_header_start, request.content_type[: len(multipart_header_start)])
assert_with_message("message", "Hello, world!", request.form["message"])
assert_with_message("message", "<file content>", request.form["fileContent"])
else:
return Response(status=400)
return Response(status=200)


Expand Down