Crates.io | axum-media |
lib.rs | axum-media |
version | 0.2.0 |
source | src |
created_at | 2023-11-20 13:02:48.678184 |
updated_at | 2023-11-23 18:46:04.589048 |
description | A simple way to use multiple media types with axum |
homepage | |
repository | https://github.com/marekvospel/axum-media |
max_upload_size | |
id | 1042104 |
size | 33,188 |
This library provides a way to use multiple mime types for serializing and deserializing structs within the axum ecosystem. Inspired by axum's Json extractor.
[dependencies]
# Enable features such as urlencoded
axum-media = { version = "0.2.0", features = ["urlencoded"]}
use axum_media::{AnyMedia, ContentType};
#[tokio::main]
async fn main() {
let app = axum::Router::new()
.route("/", get(index))
.route("/login", post(login))
}
async fn index(content_type: ContentType) -> impl IntoResponse {
// Chooses the right serializer based on the Accept header
AnyMedia(
serde_json::json!({
"routes": ["/", "/login"],
}),
content_type,
)
}
#[derive(Deserialize)]
struct LoginData {
email: String
password: String
}
// Automatically chooses the right deserializer based on the Content-Type header
async fn login(AnyMedia(data, _): AnyMedia<LoginData>) -> String {
data.email
}