multipart API¶
multipart
¶
Zero-dependency multipart/form-data parser and encoder.
Parses and encodes multipart/form-data bodies per RFC 7578 / RFC 2046.
Designed for HTTP file upload handling without any third-party dependencies.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
MultipartError
¶
MultipartParseError
¶
Bases: MultipartError
Raised when parsing fails (malformed input, missing boundary, etc.).
MultipartEncodeError
¶
Bases: MultipartError
Raised when encoding fails (invalid arguments).
Part
dataclass
¶
A single part from a multipart/form-data body.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Form field name from Content-Disposition. |
data |
bytes
|
Raw bytes content of this part. |
filename |
str | None
|
Original filename for file uploads, or None for text fields. |
content_type |
str
|
MIME type of this part. |
headers |
dict[str, str]
|
All MIME headers of this part (keys lowercased). |
Source code in multipart/multipart.py
extract_boundary(content_type)
¶
Extract the boundary string from a Content-Type header.
Handles both quoted and unquoted boundaries. If the string contains
no boundary=, it is returned as-is (assumed to be a bare boundary).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
content_type
|
str
|
Full Content-Type header value or bare boundary string. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The boundary string. |
Raises:
| Type | Description |
|---|---|
MultipartParseError
|
If Content-Type is multipart/* but has no boundary. |
Source code in multipart/multipart.py
parse_multipart(body, content_type, *, max_part_size=_DEFAULT_MAX_PART_SIZE, max_parts=_DEFAULT_MAX_PARTS)
¶
Parse a multipart/form-data request body into a list of parts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
body
|
bytes
|
The raw request body bytes. |
required |
content_type
|
str
|
Full Content-Type header value
(e.g. |
required |
max_part_size
|
int
|
Maximum size in bytes for any single part. Set to 0 to disable. |
_DEFAULT_MAX_PART_SIZE
|
max_parts
|
int
|
Maximum number of parts allowed. Set to 0 to disable. |
_DEFAULT_MAX_PARTS
|
Returns:
| Type | Description |
|---|---|
list[Part]
|
List of Part objects in the order they appeared. |
Raises:
| Type | Description |
|---|---|
MultipartParseError
|
If the body is malformed or limits are exceeded. |
Source code in multipart/multipart.py
encode_multipart(fields=None, files=None, *, boundary=None)
¶
Encode form fields and files as a multipart/form-data body.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fields
|
dict[str, str | bytes] | list[tuple[str, str | bytes]] | None
|
Text form fields as |
None
|
files
|
dict[str, bytes | tuple[str, bytes] | tuple[str, bytes, str]] | list[tuple[str, bytes | tuple[str, bytes] | tuple[str, bytes, str]]] | None
|
File uploads as dict or list of tuples. Each value can be:
|
None
|
boundary
|
str | None
|
Optional boundary string. If None, a random one is generated. |
None
|
Returns:
| Type | Description |
|---|---|
bytes
|
|
str
|
the full |
Raises:
| Type | Description |
|---|---|
MultipartEncodeError
|
If arguments are invalid. |
Source code in multipart/multipart.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |