[−][src]Struct sputnik::request::Body
Convenience wrapper around hyper::Body
.
Implementations
impl Body
[src]
pub async fn into_bytes(self) -> Result<Bytes, Error>
[src]
pub async fn into_form<T: DeserializeOwned>(self) -> Result<T, Error>
[src]
Parses a application/x-www-form-urlencoded
request body into a given struct.
This does make you vulnerable to CSRF so you normally want to use
[parse_form_csrf()
] instead.
Example
use hyper::{Response}; use sputnik::{request::Body, Error}; use serde::Deserialize; #[derive(Deserialize)] struct Message {text: String, year: i64} async fn greet(body: Body) -> Result<Response<hyper::Body>, Error> { let msg: Message = body.into_form().await?; Ok(Response::new(format!("hello {}", msg.text).into())) }
pub async fn into_form_csrf<T: DeserializeOwned>(
self,
csrf_token: &CsrfToken
) -> Result<T, Error>
[src]
self,
csrf_token: &CsrfToken
) -> Result<T, Error>
Parses a application/x-www-form-urlencoded
request body into a given struct.
Protects from CSRF by checking that the request body contains the same token retrieved from the cookies.
The CSRF parameter is expected as the csrf
parameter in the request body.
This means for HTML forms you need to embed the token as a hidden input.
Example
use hyper::{Method}; use sputnik::{request::{Parts, Body}, response::Response, Error}; use sputnik::security::CsrfToken; use serde::Deserialize; #[derive(Deserialize)] struct Message {text: String} async fn greet(req: &mut Parts, body: Body) -> Result<Response, Error> { let mut response = Response::new(); let csrf_token = CsrfToken::from_parts(req, &mut response); *response.body() = match (req.method()) { &Method::GET => format!("<form method=post> <input name=text>{}<button>Submit</button></form>", csrf_token.html_input()).into(), &Method::POST => { let msg: Message = body.into_form_csrf(&csrf_token).await?; format!("hello {}", msg.text).into() }, _ => return Err(Error::method_not_allowed("only GET and POST allowed".to_owned())), }; Ok(response) }
Auto Trait Implementations
impl !RefUnwindSafe for Body
[src]
impl Send for Body
[src]
impl Sync for Body
[src]
impl Unpin for Body
[src]
impl !UnwindSafe for Body
[src]
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
pub fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T> Instrument for T
[src]
pub fn instrument(self, span: Span) -> Instrumented<Self>
[src]
pub fn in_current_span(self) -> Instrumented<Self>
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T> Same<T> for T
type Output = T
Should always be Self
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error
The type returned in the event of a conversion error.
pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>
[src]
impl<V, T> VZip<V> for T where
V: MultiLane<T>,
V: MultiLane<T>,