Skip to content

fix(http-client-python): synthesize filename in multipart Content-Disposition for bare file inputs#10843

Open
iscai-msft wants to merge 3 commits into
microsoft:mainfrom
iscai-msft:fix/python-multipart-filename
Open

fix(http-client-python): synthesize filename in multipart Content-Disposition for bare file inputs#10843
iscai-msft wants to merge 3 commits into
microsoft:mainfrom
iscai-msft:fix/python-multipart-filename

Conversation

@iscai-msft
Copy link
Copy Markdown
Member

Problem

When callers pass bare bytes/str/IO (the FileContent variant of FileType) for multipart file fields, the generated prepare_multipart_form_data helper creates (field_name, bare_content). The HTTP library interprets this as Content-Disposition: form-data; name="field_name" with no filename= attribute.

Many servers require filename= in the Content-Disposition header to recognize file uploads (e.g., servers that use the file extension to detect package type will reject with "At least one file must be uploaded").

The tuple variants (filename, content) and (filename, content, content_type) already work correctly since they produce filename= in the header.

Fix

Added _normalize_multipart_file_entry helper in utils.py.jinja2 that wraps bare content into a (filename, content) tuple:

  • IO objects with .name: derives filename via os.path.basename() (e.g., open('path/to/image.jpg')filename="image.jpg")
  • Bare bytes/str: falls back to the field name (e.g., "profileImage") or "field_0", "field_1" for list entries
  • Existing tuples: pass through unchanged

Also changed elif multipart_entry: to elif multipart_entry is not None: to allow empty bytes (b"") to be uploaded.

Files Changed

  • generator/pygen/codegen/templates/utils.py.jinja2: Added _normalize_multipart_file_entry helper, updated prepare_multipart_form_data
  • generator/pygen/codegen/serializers/general_serializer.py: Added import os to generated utils imports

@microsoft-github-policy-service microsoft-github-policy-service Bot added the emitter:client:python Issue for the Python client emitter: @typespec/http-client-python label May 29, 2026
@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 29, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/http-client-python@10843

commit: 2d8ea5d

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 29, 2026

All changed packages have been documented.

  • @typespec/http-client-python
Show changes

@typespec/http-client-python - fix ✏️

Synthesize filename in multipart Content-Disposition for bare file inputs. When callers pass bare bytes/str/IO instead of a (filename, content) tuple for multipart file fields, the prepare_multipart_form_data helper now wraps them with a synthesized filename so servers that require filename= in the Content-Disposition header no longer reject the upload.

@azure-sdk-automation
Copy link
Copy Markdown

azure-sdk-automation Bot commented May 29, 2026

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

iscai-msft and others added 2 commits May 29, 2026 18:03
…ition

When callers pass bare bytes/str/IO instead of a (filename, content) tuple
for multipart file fields, the prepare_multipart_form_data helper now wraps
them with a synthesized filename derived from IO.name or the field name.
This ensures the Content-Disposition header includes filename= so servers
that require it (e.g. for .zip detection) don't reject the upload.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@iscai-msft iscai-msft force-pushed the fix/python-multipart-filename branch from 196355c to 4ad096a Compare May 29, 2026 22:03
@iscai-msft iscai-msft requested a review from l0lawrence as a code owner May 29, 2026 22:03
…esis

Add sync and async test variants that pass bare open() IO (without explicit
filename tuples) to the uploadFileRequiredFilename, uploadFileSpecificContentType,
and uploadFileArray Spector scenarios. These exercise _normalize_multipart_file_entry
and verify the server receives filename= in Content-Disposition.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:client:python Issue for the Python client emitter: @typespec/http-client-python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant