Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 4, 2025

Per RFC 1341 (MIME) 7.2, multipart messages must contain one or more body parts. MultipartContent with zero parts serializes as a single dummy empty part (no headers, no body) to satisfy this requirement, but other HTTP clients may not interpret this correctly.

Changes

  • Added remarks to MultipartContent documenting the zero-part serialization behavior
  • Clarified RFC compliance and backward compatibility rationale
  • Added recommendation to include at least one part before serializing

Example

await new MultipartContent("mixed", "111").ReadAsStringAsync();
// Outputs:
// --111
// 
// --111--
// 
// (A single empty part between boundaries)

Addresses #11526

Original prompt

This section details on the original issue you should resolve

<issue_title>MultipartContent with zero parts writes itself as a dummy empty part</issue_title>
<issue_description>### Description

RFC 1341 (MIME) 7.2 and all its successors state that multipart messages must contain one or more body parts. A multipart message with no body parts, i.e. consisting of only the closing multipart delimiter, violates the standard. Probably for this reason, System.Net.Http.MultipartContent with zero parts added to it writes itself as a dummy empty part with no content headers or message body. This output follows the letter of the RFC, but may not be what users of MultipartContent without deep knowledge of the RFC expect to happen. Multipart reader implementation in HttpContentMultipartExtension from System.Net.Http.Formatting has a special check to read the resulting message as a multipart content with zero parts (it discards the dummy empty part).

However, (a) other HTTP clients may not interpret this message in the same way, potentially leading to unexpected results, and (b) the RFC does not appear to single out empty parts with no headers or content as being in any way special (chapter 7.2 specifically states that in body parts all headers are optional), so this looks like a backward compatibility. As such, it is almost certainly going to stay that way, although it would probably be more correct for a MultipartContent with zero parts added to it to throw an exception when written out.

Reproduction Steps

Console.WriteLine (await new MultipartContent ("mixed", "111").ReadAsStringAsync ());
prints

--111

--111--

Expected behavior

I suggest adding a sentence to the description of MultipartContent to document this behavior.

Actual behavior

A MultipartContent with zero parts added writes itself as a single dummy empty body part.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI and others added 3 commits November 4, 2025 16:24
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Co-authored-by: MihaZupan <25307628+MihaZupan@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix dummy empty part in MultipartContent with zero parts Document MultipartContent zero-part serialization behavior Nov 4, 2025
Copilot AI requested a review from MihaZupan November 4, 2025 16:32
Copilot finished work on behalf of MihaZupan November 4, 2025 16:32
@MihaZupan MihaZupan marked this pull request as ready for review November 4, 2025 17:15
@MihaZupan MihaZupan requested a review from a team as a code owner November 4, 2025 17:15
Copilot AI review requested due to automatic review settings November 4, 2025 17:15
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This pull request adds documentation to the MultipartContent class in the System.Net.Http namespace, specifically enhancing the <remarks> section which was previously a placeholder.

Key Changes

  • Added detailed remarks explaining the behavior of MultipartContent when serialized with zero body parts
  • Documented the workaround for servers that don't support empty multipart content

Comment on lines +58 to +60
## Remarks
Multipart messages must contain one or more body parts. When a <xref:System.Net.Http.MultipartContent> instance with zero parts is serialized, it's written as a single empty part with no content headers or message body.
If your server does not support such contents with zero parts, consider conditionally using a different or no `HttpContent`.
Copy link

Copilot AI Nov 4, 2025

Choose a reason for hiding this comment

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

The sentences in lines 59 and 60 should end with periods to be consistent with documentation standards.

Copilot generated this review using guidance from repository custom instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MultipartContent with zero parts writes itself as a dummy empty part

2 participants