[−][src]Struct sputnik::Request
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]
&mut 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, 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
Auto Trait Implementations
impl !RefUnwindSafe for Request
[src]
impl Send for Request
[src]
impl Sync for Request
[src]
impl Unpin for Request
[src]
impl !UnwindSafe for Request
[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>,