[][src]Struct sputnik::Request

pub struct Request { /* fields omitted */ }

Convenience wrapper around hyper::Request.

Implementations

impl Request[src]

pub fn cookies(&mut self) -> &HashMap<String, Cookie<'_>>[src]

pub fn method(&self) -> &Method[src]

pub fn uri(&self) -> &Uri[src]

pub async fn into_form<T: DeserializeOwned>(&mut 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, Body};
use sputnik::{Request, Error};
use serde::Deserialize;

#[derive(Deserialize)]
struct Message {text: String, year: i64}

async fn greet(req: &mut Request) -> Result<Response<Body>, Error> {
    let msg: Message = req.into_form().await?;
    Ok(Response::new(format!("hello {}", msg.text).into()))
}

pub async fn into_form_csrf<T: DeserializeOwned>(
    &mut 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, Response, Error};
use sputnik::security::CsrfToken;
use serde::Deserialize;

#[derive(Deserialize)]
struct Message {text: String}

async fn greet(req: &mut Request) -> Result<Response, Error> {
    let mut response = Response::new();
    let csrf_token = CsrfToken::from_request(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 = req.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)
}

pub async fn into_body(&mut self) -> Result<Bytes, Error>[src]

pub fn query<T: DeserializeOwned>(&self) -> Result<T, Error>[src]

Parses the query string of the request into a given struct.

Trait Implementations

impl From<Request<Body>> for Request[src]

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>,