| Crates.io | axum-params |
| lib.rs | axum-params |
| version | 0.4.1 |
| created_at | 2024-12-27 17:29:11.245118+00 |
| updated_at | 2025-04-16 02:28:19.948567+00 |
| description | A Rails-like powerful parameter handling library for Axum |
| homepage | |
| repository | https://github.com/cpunion/axum-params |
| max_upload_size | |
| id | 1496513 |
| size | 166,232 |
A powerful parameter handling library for Axum web framework, inspired by Ruby on Rails' parameter system. Seamlessly handles multiple parameter sources and tree-structured data with file uploads.
Unified Parameter Handling
Rails-like Tree-Structured Parameters
post[attachments][][file]Example structure:
post: {
title: String,
content: String,
tags: Vec<String>,
cover: UploadFile,
attachments: Vec<{
file: UploadFile,
description: String
}>
}
Add this to your Cargo.toml:
[dependencies]
axum-params = "0.4"
use axum::{routing::post, Router};
use axum_params::Params;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
struct Attachment {
file: UploadFile,
description: String,
}
#[derive(Serialize, Deserialize)]
struct CreatePost {
title: String,
content: String,
tags: Vec<String>,
cover: UploadFile,
attachments: Vec<Attachment>,
}
#[debug_handler]
async fn create_post_handler(Params(post, _): Params<CreatePost>) -> impl IntoResponse {
// Handle cover file
let mut cover_file = post.cover.open().await.unwrap();
// process file
// Handle attachments
for attachment in post.attachments {
let mut file = attachment.file.open().await.unwrap();
// process file
}
}
Just like Rails, you can send nested parameters in various formats:
# Combining path parameters, query parameters, and form data
curl -X POST "http://localhost:3000/posts/123?draft=true" \
-F "post[title]=My First Post" \
-F "post[content]=Hello World from Axum" \
-F "post[tags][]=rust" \
-F "post[cover]=@cover.jpg"
# Sending JSON data (note: file uploads not possible in pure JSON)
curl -X POST http://localhost:3000/posts \
-H "Content-Type: application/json" \
-d '{
"post": {
"title": "My First Post",
"content": "Hello World from Axum",
"tags": ["rust", "web", "axum"]
}
}'
# Basic form data with nested parameters and file uploads
curl -X POST http://localhost:3000/posts \
-F "post[title]=My First Post" \
-F "post[content]=Hello World from Axum" \
-F "post[tags][]=rust" \
-F "post[tags][]=web" \
-F "post[tags][]=axum" \
-F "post[cover]=@cover.jpg" \
-F "post[attachments][][file]=@document.pdf" \
-F "post[attachments][][description]=Project Documentation" \
-F "post[attachments][][file]=@diagram.png" \
-F "post[attachments][][description]=Architecture Diagram"
The library automatically handles multipart form data, allowing you to upload files within nested structures. Files can be placed at any level in the parameter tree, and you can combine them with regular form fields.
# Complex multipart form matching the Post struct example
curl -X POST http://localhost:3000/posts \
-F "post[title]=My First Post" \
-F "post[content]=Hello World from Axum" \
-F "post[tags][]=rust" \
-F "post[tags][]=web" \
-F "post[tags][]=axum" \
-F "post[cover]=@cover.jpg" \
-F "post[attachments][][file]=@document.pdf" \
-F "post[attachments][][description]=Project Documentation" \
-F "post[attachments][][file]=@diagram.png" \
-F "post[attachments][][description]=Architecture Diagram" \
-F "post[attachments][][file]=@screenshot.jpg" \
-F "post[attachments][][description]=Application Screenshot"
This example demonstrates how the multipart form maps to the Rust struct:
title, content)tags[])cover)attachments[] with file and description)# Run basic parameters example
cargo run --example basic_params
# Run file upload example
cargo run --example file_upload
# Run nested parameters example
cargo run --example nested_params
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.