The multipart feature flag

Learn what the multipart feature flag does in Axum, how to turn it on in Cargo.toml, and how to check that it worked

A file upload travels to your server inside a request body that follows a format called multipart/form-data. Axum knows how to read that format, but the code that does it is switched off by default. The multipart feature flag is the switch. You add one word to your Cargo.toml, and Axum compiles in the upload support. Without that word, the upload types do not exist at all, and the compiler tells you so.

Where This Fits

A file upload needs two things from Axum. First, the crate has to include the code that reads multipart bodies. Second, your handler has to use that code to pull fields out of the request. This page covers only the first half, turning the code on. The handler half comes right after it, one idea at a time.

Think of a feature flag like a light switch for one room of a house. The wiring for the room is already in the walls, but the switch is off, so the room stays dark. Adding features = ["multipart"] flips that one switch. Nothing else in the house changes, and the rest of Axum works exactly as before.

The Smallest Working Version

Open the Cargo.toml of your project and find the axum line in the dependencies. Change it so it carries the feature:

Cargo.toml
[package]
name = "upload-demo"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = { version = "0.8", features = ["multipart"] }
tokio = { version = "1", features = ["full"] }

That is the whole change. The next step is to prove the switch is on by using the type it unlocks. Add a handler that takes a Multipart value, even if it does nothing with it yet:

src/main.rs
use axum::{extract::Multipart, routing::post, Router};

async fn upload(_multipart: Multipart) -> &'static str {
    "got the upload"
}

#[tokio::main]
async fn main() {
    let app = Router::new().route("/upload", post(upload));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    println!("Server running on http://127.0.0.1:3000");
    axum::serve(listener, app).await.unwrap();
}

Run the check command, which verifies the code compiles without starting the server:

cargo check

When the output ends with a Finished line and no errors, the flag is on and the Multipart type exists. The handler does nothing with the upload yet, so sending it a file returns the plain text got the upload. Reading the fields out of it is the work of the pages that follow, and this page only proves the door is open.

The flag is on when cargo check passes:

If cargo check finishes cleanly with the use axum::extract::Multipart line in place, the feature flag is working. A clean check is the fastest proof, and you need nothing else before moving on.

What a Feature Flag Is

A feature flag is a named option on a crate that turns a piece of functionality on or off at compile time. Compile time means the moment Cargo builds your project, before the program ever runs. When a feature is off, the code behind it is left out of the build entirely. When it is on, the code is compiled in and its types become available to your code.

Axum is built from many optional pieces. Routing, JSON handling, and the multipart reader are separate pieces, and each has its own flag. This keeps small programs small. A server that only returns JSON never pays for upload parsing code it never uses, because that code is never compiled into it. A server that does accept uploads opts in with one word and gets exactly that piece.

The word goes in the dependency entry, inside the curly braces, as features = ["multipart"]. The square brackets hold a list, because a dependency can carry more than one feature at once. The feature name is multipart, exactly that spelling, in lowercase, with no extra words.

A feature is not a separate crate:

Turning on multipart does not add a new dependency you manage yourself. Axum brings in the parsing library it needs on its own. You never write multer in your own Cargo.toml to use Axum uploads, and you never import anything from it in a handler.

What the Flag Pulls In

Behind the flag, Axum depends on a small parsing crate called multer, version 3.1.0, which reads the multipart format. The Multipart type you use in a handler is Axum's own wrapper around that parser. You work with Axum's type, and the parser stays out of sight.

You can see this for yourself after the first build. Cargo writes a Cargo.lock file that records the exact version of every crate in the build, and it lists multer version 3.1.0 there. That file is written by Cargo, and you never edit it by hand. Its presence is just a way to confirm which version your build actually used.

The versions in the smallest example above are the current ones, confirmed while writing this page: axum 0.8.9, tokio 1.54.1, and multer 3.1.0. Writing axum = "0.8" in Cargo.toml means any 0.8 release is acceptable, and Cargo picks the newest one that fits, which is 0.8.9. Writing the full 0.8.9 works too, and it pins the choice more tightly. Both spellings turn the flag on the same way.

You can also confirm the flag from the built dependency tree. The cargo tree command prints the crates in the build and the features each one was built with:

cargo tree -e features -i multer
multer v3.1.0
+-- multer feature "default"
    +-- axum v0.8.9
        |-- axum feature "default"
        |   +-- upload-demo v0.1.0
        |-- axum feature "multipart"
        |   +-- upload-demo v0.1.0
        ...

The output above shows the shape of the tree in a project named upload-demo, trimmed to the interesting lines. It shows multer v3.1.0 in the tree, reached through axum feature "multipart", which is the proof that the flag is what pulled the parser in. If multer does not appear in that output, the flag is not enabled in the build, whatever the Cargo.toml seems to say.

The Error You Get Without the Flag

The most useful way to understand the flag is to see what happens without it. Take a project whose Cargo.toml has a plain axum = "0.8.9" line with no features, and add the same use axum::extract::Multipart; line to main.rs. Running cargo check then fails with this error:

error[E0432]: unresolved import `axum::extract::Multipart`
   |                    no `Multipart` in `extract`

The compiler is saying that the extract module has no Multipart in it. That is not a typo in your import and not a missing dependency. The type is genuinely absent from the build, because the flag that includes it is off. The fix is the one line in Cargo.toml, not a change to the Rust code.

This error is worth recognising, because it looks like an import problem and tempts a beginner into fixing the wrong thing. If you see no 'Multipart' in 'extract', check the feature flag first. The same shape of error appears for other feature-gated types in Axum, and the cure is always the same, add the feature to the dependency.

The import error means the flag is off:

error[E0432]: unresolved import 'axum::extract::Multipart' is the compiler telling you the multipart feature is not enabled. Do not rewrite the import, do not add another crate, and do not change the handler. Add features = ["multipart"] to the axum line in Cargo.toml and run cargo check again.

Adding the Flag to a Project That Already Exists

Most of the time you are not starting a fresh project. You have an axum line already, and you need to add the flag to it. Find the existing line and extend it, keeping everything else on that line as it is:

Cargo.toml
axum = { version = "0.8", features = ["multipart"] }

If your project already enables other axum features, add multipart to the same list, separated by a comma:

Cargo.toml
axum = { version = "0.8", features = ["json", "multipart"] }

There is exactly one place to make this change, the [dependencies] section of the Cargo.toml at the root of your project. A workspace with several crates has one Cargo.toml per crate, and the flag belongs in the Cargo.toml of the crate whose code uses Multipart. Editing the wrong file changes nothing and produces the same import error as before.

After editing, run cargo check again. Cargo notices the changed feature list and rebuilds Axum with the new piece included. The first build after flipping the flag takes longer than usual, because Axum and the parser crate are compiled fresh. Later builds go back to normal speed.

A Feature Flag Is a Compile Time Choice

A detail that confuses beginners is that a feature flag cannot be changed while the server is running. It is decided when Cargo builds the project, and the built program carries that decision with it. To turn uploads on for a deployed server, you change Cargo.toml, rebuild, and restart the server with the new binary. There is no setting, environment variable, or request that enables it at runtime.

The same is true in reverse. Removing the flag and rebuilding takes the upload code back out, and any handler that uses Multipart then fails to compile. That is a useful guard. It means you can never accidentally deploy a server that accepts uploads without the parsing code, because the build itself refuses.

How the Pieces Connect

The path from the flag to a working upload looks like this:

Cargo.toml lists axum with features = ["multipart"]
        |
        v
Cargo builds axum with the multipart code included
        |
        v
multer 3.1.0 is compiled in as the parser underneath
        |
        v
axum::extract::Multipart exists and can be imported
        |
        v
A handler takes Multipart and reads the fields

Every step depends on the one above it. If the first line is missing, nothing below it exists, and the compiler stops at the import. If the first line is present, the rest follows without any further configuration.

Common Beginner Mistakes

Adding a second axum line instead of extending the first

# Wrong - two entries for the same crate
[dependencies]
axum = "0.8"
axum = { version = "0.8", features = ["multipart"] }

A Cargo.toml cannot list the same dependency twice. Cargo rejects the file before it even starts resolving versions. The fix is to keep one line and put the feature on it, as the smallest example shows.

Misspelling the feature name

The feature is called multipart. Names like multipart-form-data, multipart-upload, or upload do not exist, and Cargo fails to resolve the dependency with an error saying the feature does not exist. If the build fails on feature resolution, read the feature name character by character before changing anything else.

Editing the wrong Cargo.toml

In a workspace, or in a folder with an old copy of the project, it is easy to edit a Cargo.toml that the build never reads. The symptom is that the import error survives the edit. Run cargo check from the project root, the folder that holds the Cargo.toml you edited, and check that the file you changed is the one in that folder.

Expecting the flag to change runtime behaviour on its own

Turning the flag on does not create an upload route, does not change any existing route, and does not make the server accept files. It only makes the Multipart type available. A server with the flag on and no handler that uses it behaves exactly like a server with the flag off. The flag is permission to write upload code, not upload code itself.

One axum line, one feature list:

Keep a single axum entry in [dependencies] and put every feature you need in its one list. A duplicate entry is a hard error, and a misspelled feature name fails resolution, so both mistakes stop the build before any of your code is checked.

Checking That Everything Compiles

After the smallest example, run the full check and build once:

cargo check
cargo run

cargo check verifies the code compiles, and cargo run builds the binary and starts the server. The server prints Server running on http://127.0.0.1:3000 and waits. Sending it a file at this stage returns the plain text got the upload, because the handler ignores the fields. That response is the proof that the route, the extractor, and the flag all work together. The next page starts reading the fields the handler is currently ignoring.

Final Complete Example

Here is everything from this page in one place, the dependency that turns the flag on and the smallest server that proves it:

Cargo.toml
[package]
name = "upload-demo"
version = "0.1.0"
edition = "2021"

[dependencies]
axum = { version = "0.8", features = ["multipart"] }
tokio = { version = "1", features = ["full"] }
src/main.rs
use axum::{extract::Multipart, routing::post, Router};

async fn upload(_multipart: Multipart) -> &'static str {
    "got the upload"
}

#[tokio::main]
async fn main() {
    let app = Router::new().route("/upload", post(upload));

    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
        .await
        .unwrap();
    println!("Server running on http://127.0.0.1:3000");
    axum::serve(listener, app).await.unwrap();
}

Run it with cargo run, then send a file with curl from a second terminal:

curl -X POST http://127.0.0.1:3000/upload \
  -F "avatar=@photo.png;type=image/png"

The -F flag tells curl to send a multipart body with a field named avatar holding the contents of photo.png. The server answers with the plain text got the upload and status 200. The file arrived, the extractor accepted it, and the handler ignored its contents. Every page after this one is about what the handler does instead of ignoring it.

Summary

The multipart feature flag is the cheapest decision in the whole upload flow, one word in Cargo.toml, and it is also the easiest to misread when it goes wrong. The compiler error for a missing flag looks like a broken import, but it is really a build configuration problem, and the fix lives in the dependency list, not in the Rust code. That split is worth keeping in mind as a general rule: when a type that the docs say exists cannot be found, check whether a feature flag gates it before rewriting any code. The flag itself does nothing at runtime. It only decides what gets compiled in, which is why turning it on changes nothing about a running server until a handler actually uses the type it unlocks. The useful test is always the same, a clean cargo check with the import in place, and from there the work moves to the handler, where the fields are read one at a time.