pub struct Xml<T>(pub T);
Available on crate feature
xml
only.Expand description
XML extractor and response.
To extract the specified type of XML from the body, T
must implement
serde::Deserialize
.
Errors
use poem::{
handler,
http::{header, Method, StatusCode},
post,
test::TestClient,
web::Xml,
Endpoint, Request, Route,
};
use serde::Deserialize;
#[derive(Deserialize)]
struct User {
name: String,
}
#[handler]
async fn index(Xml(user): Xml<User>) -> String {
format!("welcome {}!", user.name)
}
let app = Route::new().at("/", post(index));
let cli = TestClient::new(app);
let resp = cli
.post("/")
.header(header::CONTENT_TYPE, "application/xml")
.body(r#"<User name="foo"/>"#)
.send()
.await;
resp.assert_status_is_ok();
resp.assert_text("welcome foo!").await;
Response
To serialize the specified type to XML, T
must implement
serde::Serialize
.
use poem::{
get, handler, http::StatusCode, test::TestClient, web::Xml, Endpoint, Request, Route,
};
use serde::Serialize;
#[derive(Serialize)]
struct User {
name: String,
}
#[handler]
async fn index() -> Xml<User> {
Xml(User {
name: "foo".to_string(),
})
}
let app = Route::new().at("/", get(index));
let cli = TestClient::new(app);
let resp = cli.get("/").send().await;
resp.assert_status_is_ok();
resp.assert_text(r#"<User name="foo"/>"#).await;
Tuple Fields
0: T
Trait Implementations
sourceimpl<'a, T: DeserializeOwned> FromRequest<'a> for Xml<T>
impl<'a, T: DeserializeOwned> FromRequest<'a> for Xml<T>
sourcefn from_request<'life0, 'async_trait>(
req: &'a Request,
body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
fn from_request<'life0, 'async_trait>(
req: &'a Request,
body: &'life0 mut RequestBody
) -> Pin<Box<dyn Future<Output = Result<Self>> + Send + 'async_trait>>where
'a: 'async_trait,
'life0: 'async_trait,
Self: 'async_trait,
Extract from request head and body.
sourceimpl<T: Serialize + Send> IntoResponse for Xml<T>
impl<T: Serialize + Send> IntoResponse for Xml<T>
sourcefn into_response(self) -> Response
fn into_response(self) -> Response
Consume itself and return
Response
.sourcefn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Self: Sized,
fn with_header<K, V>(self, key: K, value: V) -> WithHeader<Self>where
K: TryInto<HeaderName>,
V: TryInto<HeaderValue>,
Self: Sized,
Wrap an
impl IntoResponse
to add a header. Read moresourcefn with_content_type<V>(self, content_type: V) -> WithContentType<Self>where
V: TryInto<HeaderValue>,
Self: Sized,
fn with_content_type<V>(self, content_type: V) -> WithContentType<Self>where
V: TryInto<HeaderValue>,
Self: Sized,
Wrap an
impl IntoResponse
to with a new content type. Read moresourcefn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
fn with_status(self, status: StatusCode) -> WithStatus<Self>where
Self: Sized,
Wrap an
impl IntoResponse
to set a status code. Read moreimpl<T: Eq> Eq for Xml<T>
impl<T> StructuralEq for Xml<T>
impl<T> StructuralPartialEq for Xml<T>
Auto Trait Implementations
impl<T> RefUnwindSafe for Xml<T>where
T: RefUnwindSafe,
impl<T> Send for Xml<T>where
T: Send,
impl<T> Sync for Xml<T>where
T: Sync,
impl<T> Unpin for Xml<T>where
T: Unpin,
impl<T> UnwindSafe for Xml<T>where
T: UnwindSafe,
Blanket Implementations
impl<T, A, P> Access<T> for Pwhere
A: Access<T>,
P: Deref<Target = A>,
impl<T, A, P> Access<T> for Pwhere
A: Access<T>,
P: Deref<Target = A>,
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T, A> DynAccess<T> for Awhere
A: Access<T>,
<A as Access<T>>::Guard: 'static,
impl<T, A> DynAccess<T> for Awhere
A: Access<T>,
<A as Access<T>>::Guard: 'static,
fn load(&self) -> DynGuard<T>
fn load(&self) -> DynGuard<T>
The equivalent of [
Access::load
].sourceimpl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to
key
and return true
if they are equal.sourceimpl<T> FutureExt for T
impl<T> FutureExt for T
sourcefn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
sourcefn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
sourceimpl<T> Instrument for T
impl<T> Instrument for T
sourcefn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
sourcefn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
sourceimpl<T> IntoResult<T> for Twhere
T: IntoResponse,
impl<T> IntoResult<T> for Twhere
T: IntoResponse,
sourcefn into_result(self) -> Result<T, Error>
fn into_result(self) -> Result<T, Error>
Consumes this value returns a
poem::Result<T>
.