Struct arc_reactor::core::Request[][src]

pub struct Request { /* fields omitted */ }

The Request Struct, This is passed to Middlewares and route handlers.

Methods

impl Request
[src]

Returns a reference to the request's Version

Returns a reference to the request's headers

Returns a reference to the request's method

The methods can be any of the following: GET, HEAD, POST, PUT DELETE, CONNECT, OPTIONS, TRACE, PATCH.

Returns a request to the request's Uri

Returns the query path of the request.

Returns the IP of the connected client. This should always be set, except in testing environments with FakeReactor.

Serializes the query string into a struct via serde.

Examples

This example is not tested
extern crate serde;
#[macro_use]
extern crate serde_derive;

extern crate arc_reactor;
use arc_reactor::prelude::*;

#[derive(Serialize, Deserialize)]
struct AccessToken {
    token: String,
}

#[service]
pub fn login(req: Request, _res: Response) {
    if let Ok(AccessToken { token }) = req.query() {
        // do something with the token here.
    }
}

Get the url params for the request.

e.g /profile/:id

This example is not tested
extern crate arc_reactor;
use arc_reactor::prelude::*;

[service]
pub fn ProfileService(req: Request, res: Response) {
    let profileId = req.params().unwrap()["id"];
    // Its safe to unwrap here as this woute would never be matched without the `id`
}

The request struct constains an AnyMap so that middlewares can append additional information.

You can get values out of the AnyMap by using this method.

Examples

This example is not tested
extern crate arc_reactor;
use arc_reactor::prelude::*;
#[derive(Serialize, Deserialize)]
struct AccessToken {
    token: String,
}

struct User {
    name: String,
}

#[middleware(Request)]
pub fn AssertAuth(req: Request) {
    if let AccessToken { token } = req.query::<AccessToken>() {
        let user = User { name: "Seun" };
        req.set::<User>(user); // Set the user
    } else {
        return Err((401, "Unauthorized!").into());
    }
}

#[service]
pub fn ProfileService(req: Request, res: Response) {
    let user = req.get::<User>().unwrap();
    // Its safe to unwrap here, because if user isn't set this service will never
    // be called.
}

Set a type on the request.

Removes the type previously set on the request.

Move the request body

Serialize the request's json value into a struct.

Note that the json value needs to have been previously set on the request by a middleware; otherwise this would return Err(JsonError::None).

Get a reference to the request body.

Get a reference to the request body.

Set the request body.

Trait Implementations

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

Performs the conversion.

impl<T> MiddleWare<Request> for T where
    T: Fn(Request) -> MiddleWareFuture<Request> + Send + Sync + Clone + 'static, 
[src]

impl MiddleWare<Request> for Vec<Box<MiddleWare<Request>>>
[src]

This enables a vector of MiddleWare<Request> to behave like a single MiddleWare<Request> returning Err(Response) in any of the MiddleWare<Request> will cause the rest of the middlewares to be skipped. Note that there's a conveinience macro mw that allows you not write boxes everywhere.

impl MiddleWare<Request> for Box<MiddleWare<Request>>
[src]

impl MiddleWare<Request> for BodyParser
[src]

impl MiddleWare<Request> for StaticFileServer
[src]

impl Debug for Request
[src]

Formats the value using the given formatter. Read more

Auto Trait Implementations

impl Send for Request

impl !Sync for Request