Crates.io | axum-xml-up |
lib.rs | axum-xml-up |
version | 0.2.0 |
source | src |
created_at | 2023-09-23 04:57:58.184346 |
updated_at | 2023-12-14 03:39:48.153881 |
description | XML extractor for axum |
homepage | |
repository | https://github.com/PhotonQuantum/axum-xml |
max_upload_size | |
id | 981058 |
size | 16,269 |
Fork of https://github.com/PhotonQuantum/axum-xml updating it to the latest axum 0.7 version for axum 0.6 use v0.1 of this library
XML extractor for axum.
This crate provides struct Xml
that can be used to extract typed information from request's body.
Uses quick-xml to deserialize and serialize the payloads
[dependencies]
axum-xml-up = "0.2"
encoding
: support non utf-8 payloadWhen used as an Extractor XML content can be deserialized from the request body into some type that implements serde::Deserialize
. If the request body cannot be parsed, or it does not contain the Content-Type: application/xml
header, it will reject the request and return a 400 Bad Request
response.
use axum::{
extract,
routing::post,
Router,
};
use serde::Deserialize;
use axum_xml_up::Xml;
#[derive(Deserialize)]
struct CreateUser {
email: String,
password: String,
}
async fn create_user(Xml(payload): Xml<CreateUser>) {
// payload is a `CreateUser`
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/users", post(create_user));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
When used as a response, it can serialize any type that implements serde::Serialize
to XML
, and will automatically set Content-Type: application/xml
header.
use axum::{
extract,
routing::get,
Router,
};
use serde::Deserialize;
use axum_xml_up::Xml;
#[derive(Deserialize)]
struct User {
id: u32,
username: String,
}
async fn get_user(Path(user_id) : Path<u32>) -> Xml<User> {
let user = find_user(user_id).await;
Xml(user)
}
async fn find_user(user_id: u32) -> User {
unimplemented!();
}
#[tokio::main]
async fn main() {
let app = Router::new().route("/users", get(get_user));
let listener = tokio::net::TcpListener::bind("0.0.0.0:3000").await.unwrap();
axum::serve(listener, app).await.unwrap();
}
MIT