From 6e0abe9459842daddaa78d85fcf584b3a43cfe81 Mon Sep 17 00:00:00 2001 From: Thomas Date: Sun, 22 Jun 2025 19:19:50 +0200 Subject: [PATCH] :bug: 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 --- fastapi/dependencies/utils.py | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/fastapi/dependencies/utils.py b/fastapi/dependencies/utils.py index 84dfa4d03..947ff2244 100644 --- a/fastapi/dependencies/utils.py +++ b/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