Browse Source

🐛 Fix handling uploaded file data correctly when not in the first part of a multipart/form-data request body

The HTTP specification for multipart/form-data, defined in RFC 7578, allows both form data and uploaded files in a single request. It does not require files to be before or after form fields.

As such, no specific care is to be given to the first field.

References:
- [RFC 7578](https://datatracker.ietf.org/doc/html/rfc7578)
- [FastAPI issue 9116](https://github.com/fastapi/fastapi/discussions/9116)

Close #9116
pull/11194/head
Thomas 3 weeks ago
committed by Thomas LEVEIL
parent
commit
6e0abe9459
  1. 7
      fastapi/dependencies/utils.py

7
fastapi/dependencies/utils.py

@ -843,20 +843,19 @@ async def _extract_form_body(
received_body: FormData,
) -> Dict[str, Any]:
values = {}
first_field = body_fields[0]
first_field_info = first_field.field_info
for field in body_fields:
value = _get_multidict_value(field, received_body)
field_info = field.field_info
if (
isinstance(first_field_info, params.File)
isinstance(field_info, params.File)
and is_bytes_field(field)
and isinstance(value, UploadFile)
):
value = await value.read()
elif (
is_bytes_sequence_field(field)
and isinstance(first_field_info, params.File)
and isinstance(field_info, params.File)
and value_is_sequence(value)
):
# For types

Loading…
Cancel
Save