Skip to content
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

[core] aiohttp multipart/form-data drops data when files included #32473

Closed
kristapratico opened this issue Oct 12, 2023 · 0 comments · Fixed by #32543
Closed

[core] aiohttp multipart/form-data drops data when files included #32473

kristapratico opened this issue Oct 12, 2023 · 0 comments · Fixed by #32543
Labels
Azure.Core bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library.

Comments

@kristapratico
Copy link
Member

kristapratico commented Oct 12, 2023

If both files and data are passed in the request, only what's in files gets added to aiohttp.FormData in the code below. data gets dropped and is not encoded into request.

def _get_request_data(self, request):
"""Get the request data.
:param request: The request object
:type request: ~azure.core.pipeline.transport.HttpRequest or ~azure.core.rest.HttpRequest
:rtype: bytes or ~aiohttp.FormData
:return: The request data
"""
if request.files:
form_data = aiohttp.FormData()
for form_file, data in request.files.items():
content_type = data[2] if len(data) > 2 else None
try:
form_data.add_field(form_file, data[1], filename=data[0], content_type=content_type)
except IndexError as err:
raise ValueError("Invalid formdata formatting: {}".format(data)) from err
return form_data
return request.data

For example, if I'm doing multipart/form-data with files and data={"response_format": "srt"}, I would expect my request body to look like:

b'--df13136170d9e03a0e82350af6b2afb7\r\nContent-Disposition: form-data; name="response_format"\r\n\r\nsrt\r\n--df13136170d9e03a0e82350af6b2afb7\r\nContent-Disposition: form-data; name="file"; filename="hello.m4a"\r\nContent-Type: application/octet-stream\r\n\r\n\x00\x00\x00\x18ftypmp42\x00\x00\x00\x00mp41isom\x00\x00\x00(uuid\\\xa7\x08\xfb2\x8eB\x05\xa8ae\x0e\xca\n\x95\x96\x00\x00\x00\x0c10.0.22621.0\x00\

Updating the code to something like the below seems to work.

if request.files:
    form_data = aiohttp.FormData(request.data or {})
@kristapratico kristapratico added Client This issue points to a problem in the data-plane of the library. Azure.Core bug This issue requires a change to an existing behavior in the product in order to be resolved. labels Oct 12, 2023
@github-actions github-actions bot locked and limited conversation to collaborators Jan 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Azure.Core bug This issue requires a change to an existing behavior in the product in order to be resolved. Client This issue points to a problem in the data-plane of the library.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant