Skip to content

[azure-core] Fix wrong urlencoded content-type for multipart requests with empty files - #49701

Open
Karan Dhaodiyal (karandhaodiyal28-hash) wants to merge 2 commits into
Azure:mainfrom
karandhaodiyal28-hash:fix-multipart-empty-files-content-type
Open

Karan Dhaodiyal (karandhaodiyal28-hash) wants to merge 2 commits into
Azure:mainfrom
karandhaodiyal28-hash:fix-multipart-empty-files-content-type

Conversation

@karandhaodiyal28-hash

Copy link
Copy Markdown

Passing files explicitly means the payload is multipart, even when no file part is present — for example Typespec-generated clients with an optional file property pass an empty files. The truthiness check in HttpRequest._set_body made that case fall into the urlencoded branch, so the request got stamped with application/x-www-form-urlencoded:

from azure.core.rest import HttpRequest

request = HttpRequest("POST", "http://example.org", data={"Hello": "world!"}, files={})
request.headers  # {'Content-Type': 'application/x-www-form-urlencoded'} — should not carry the urlencoded type

This mirrors the approach sketched by Yuchao Yan (@msyyc) in the issue: when files was explicitly passed (even empty), the urlencoded content-type must not be set, so the transport decides the multipart encoding. set_multipart_body is still only called for non-empty files, so request.files stays None in the empty case and nothing changes for existing callers.

If you'd rather have the explicit is_multipart_payload kwarg on HttpRequest as sketched in the issue, happy to add that instead.

Adds a regression test next to the existing urlencoded content-type tests.

Fixes #39163

…iles

Passing `files` explicitly means the payload is multipart, even when
no file part is present, so the urlencoded content-type header must not
be set in that case. The truthiness check on `files` made an empty
`files` value fall into the urlencoded branch and stamp
application/x-www-form-urlencoded on the request.

Fixes Azure#39163

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@github-actions github-actions Bot added Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization. labels Oct 25, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Thank you for your contribution Karan Dhaodiyal (@karandhaodiyal28-hash)! We will review the pull request and get back to you soon.

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 3 pipeline(s).
5 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

@karandhaodiyal28-hash

Copy link
Copy Markdown
Author

The remaining test failures look unrelated to this change — they're all in dependent packages, not azure-core itself: azure-mgmt-core's httpretty-mocked tests fail with ServiceResponseError: RemoteDisconnected from the socket-mock layer, and the identity/cosmos failures are likewise in their own suites. azure-core's own tests pass. Happy to re-run if useful.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community Contribution Community members are working on the issue customer-reported Issues that are reported by GitHub users external to the Azure organization.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[azure-core][bug] wrong content-type in headers of multipart request when there is no file to send

2 participants