python-multipart is an Apache2-licensed streaming multipart parser for Python. It is designed for handling `multipart/form-data` POST requests, typically used in web servers for file uploads and complex form data. The library is actively maintained with frequent releases, currently at version 0.0.22, and supports modern Python versions (>=3.10).
pip install python-multipartVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to parse a `multipart/form-data` request body using `MultipartParser`. It simulates an incoming HTTP request with form fields and a file, processing them using event-like iteration.
Update all import statements from `import multipart` or `from multipart import ...` to `from python_multipart import ...`.
Upgrade your Python environment to 3.10 or a newer supported version (e.g., 3.11, 3.12, 3.13, 3.14).
Always use `pip install python-multipart` to ensure the correct library is installed.
If your application relied on `File.filename` containing a path, adjust your logic to expect only the base name. If paths are critical, you may need to implement custom logic to extract them from the `Content-Disposition` header directly (if present and needed).
Ensure that clients sending multipart data adhere strictly to the `multipart/form-data` specification, particularly regarding the final boundary and any subsequent data.
Monitor the project's changelog and documentation for details on upcoming deprecations. Address any `PendingDeprecationWarning` messages in your code to prepare for future breaking changes.
Instead of passing a dictionary of headers, extract the `Content-Type` header value (e.g., `headers.get('Content-Type')`) and pass that string or bytes object to `MultipartParser`. For example, `parser = MultipartParser(headers.get('Content-Type').encode('latin-1'))` if your headers are strings, or `parser = MultipartParser(headers.get(b'Content-Type'))` if your headers are bytes (assuming keys are bytes).pip install python-multipart
import multipart
Ensure the `Content-Type` header is set correctly (e.g., `multipart/form-data; boundary=your_boundary_string`) and matches the boundary string present in the request body.
When working with the asynchronous `multipart.reader.MultipartReader`, ensure that all calls to its async methods (e.g., `read_part`, `next_part`) are prefixed with `await` within an `async` function.