pub struct Xml<T>(pub T);
Expand description
XML Extractor / Response.
When used as an extractor, it can deserialize request bodies into some type that
implements serde::Deserialize
. The request will be rejected (and a crate::Rejection
will
be returned) if:
- The request doesn’t have a
Content-Type: application/xml
(or similar) header. - The body doesn’t contain syntactically valid XML .
- The body contains syntactically valid XML but it couldn’t be deserialized into the target type.
- Buffering the request body fails.
⚠️ Since parsing XML requires consuming the request body, the Xml extractor must be last if there are multiple extractors in a handler.
See crate::Rejection
for more details.
§Extractor example
use axum::{
routing::post,
Router,
};
use axum_serde::Xml;
use serde::Deserialize;
#[derive(Deserialize)]
struct CreateUser {
email: String,
password: String,
}
async fn create_user(Xml(payload): Xml<CreateUser>) {
// payload is a `CreateUser`
}
let app = Router::new().route("/users", post(create_user));
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.
§Response example
use axum::{
extract::Path,
routing::get,
Router,
};
use axum_serde::Xml;
use serde::Serialize;
use uuid::Uuid;
#[derive(Serialize)]
struct User {
id: Uuid,
username: String,
}
async fn get_user(Path(user_id) : Path<Uuid>) -> Xml<User> {
let user = find_user(user_id).await;
Xml(user)
}
async fn find_user(user_id: Uuid) -> User {
// ...
}
let app = Router::new().route("/users/:id", get(get_user));
Tuple Fields§
§0: T
Implementations§
Source§impl<T> Xml<T>
impl<T> Xml<T>
Sourcepub const CONTENT_TYPE: &'static str = $content_type
pub const CONTENT_TYPE: &'static str = $content_type
Content type of XML format.
Sourcepub fn into_inner(self) -> T
pub fn into_inner(self) -> T
Consumes the Xml
extractor and returns the inner data.
Sourcepub fn from_bytes(bytes: &[u8]) -> Result<Self, Rejection<DeError>>where
T: DeserializeOwned,
pub fn from_bytes(bytes: &[u8]) -> Result<Self, Rejection<DeError>>where
T: DeserializeOwned,
Construct a Xml<T>
from a byte slice.
Most users should prefer to use the FromRequest impl but special cases may require first extracting a Request into Bytes then optionally constructing a Xml<T>
.