[][src]Struct sputnik::request::Body

pub struct Body { /* fields omitted */ }

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]

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

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T> Instrument for T[src]

impl<T> Instrument for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

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]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

impl<V, T> VZip<V> for T where
    V: MultiLane<T>,