Trait actix_web::HttpMessage [] [src]

pub trait HttpMessage {
    fn headers(&self) -> &HeaderMap;

    fn content_type(&self) -> &str { ... }
fn encoding(&self) -> Result<EncodingRef, ContentTypeError> { ... }
fn mime_type(&self) -> Result<Option<Mime>, ContentTypeError> { ... }
fn chunked(&self) -> Result<bool, ParseError> { ... }
fn range(&self, size: u64) -> Result<Vec<HttpRange>, HttpRangeError> { ... }
fn body(self) -> MessageBody<Self>
    where
        Self: Stream<Item = Bytes, Error = PayloadError> + Sized
, { ... }
fn urlencoded(self) -> UrlEncoded<Self>
    where
        Self: Stream<Item = Bytes, Error = PayloadError> + Sized
, { ... }
fn json<T: DeserializeOwned>(self) -> JsonBody<Self, T>
    where
        Self: Stream<Item = Bytes, Error = PayloadError> + Sized
, { ... }
fn multipart(self) -> Multipart<Self>
    where
        Self: Stream<Item = Bytes, Error = PayloadError> + Sized
, { ... } }

Trait that implements general purpose operations on http messages

Required Methods

Read the message headers.

Provided Methods

Read the request content type. If request does not contain Content-Type header, empty str get returned.

Get content type encoding

UTF-8 is used by default, If request charset is not set.

Convert the request content type to a known mime type.

Check if request has chunked transfer encoding

Parses Range HTTP header string as per RFC 2616. size is full size of response (file).

Load http message body.

By default only 256Kb payload reads to a memory, then PayloadError::Overflow get returned. Use MessageBody::limit() method to change upper limit.

Server example

use actix_web::*;
use bytes::Bytes;
use futures::future::Future;

fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
    req.body()                     // <- get Body future
       .limit(1024)                // <- change max size of the body to a 1kb
       .from_err()
       .and_then(|bytes: Bytes| {  // <- complete body
           println!("==== BODY ==== {:?}", bytes);
           Ok(httpcodes::HTTPOk.into())
       }).responder()
}

Parse application/x-www-form-urlencoded encoded body. Return UrlEncoded future. It resolves to a HashMap<String, String> which contains decoded parameters.

Returns error:

  • content type is not application/x-www-form-urlencoded
  • transfer encoding is chunked.
  • content-length is greater than 256k

Server example

use actix_web::*;
use futures::future::{Future, ok};

fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
    req.urlencoded()         // <- get UrlEncoded future
       .from_err()
       .and_then(|params| {  // <- url encoded parameters
            println!("==== BODY ==== {:?}", params);
            ok(httpcodes::HTTPOk.into())
       })
       .responder()
}

Parse application/json encoded body. Return JsonBody<T> future. It resolves to a T value.

Returns error:

  • content type is not application/json
  • content length is greater than 256k

Server example

use actix_web::*;
use futures::future::{Future, ok};

#[derive(Deserialize, Debug)]
struct MyObj {
    name: String,
}

fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
    req.json()                   // <- get JsonBody future
       .from_err()
       .and_then(|val: MyObj| {  // <- deserialized value
           println!("==== BODY ==== {:?}", val);
           Ok(httpcodes::HTTPOk.into())
       }).responder()
}

Return stream to http payload processes as multipart.

Content-type: multipart/form-data;

Server example

fn index(mut req: HttpRequest) -> Box<Future<Item=HttpResponse, Error=Error>> {
    req.multipart().from_err()       // <- get multipart stream for current request
       .and_then(|item| match item { // <- iterate over multipart items
           multipart::MultipartItem::Field(field) => {
               // Field in turn is stream of *Bytes* object
               Either::A(field.from_err()
                         .map(|c| println!("-- CHUNK: \n{:?}", str::from_utf8(&c)))
                         .finish())
            },
            multipart::MultipartItem::Nested(mp) => {
                // Or item could be nested Multipart stream
                Either::B(ok(()))
            }
        })
        .finish()  // <- Stream::finish() combinator from actix
        .map(|_| httpcodes::HTTPOk.into())
        .responder()
}

Implementors