axum-xml-up

Crates.ioaxum-xml-up
lib.rsaxum-xml-up
version0.2.0
sourcesrc
created_at2023-09-23 04:57:58.184346
updated_at2023-12-14 03:39:48.153881
descriptionXML extractor for axum
homepage
repositoryhttps://github.com/PhotonQuantum/axum-xml
max_upload_size
id981058
size16,269
Jacob (jacobtread)

documentation

README

axum-xml-up

crates.io Documentation

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"

Features

  • encoding: support non utf-8 payload

Request Example

When 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();
}

Response Example

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();
}

License

MIT

Commit count: 164

cargo fmt