1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 18:02:39 +01:00
actix-web/guide/src/qs_7.md
2017-12-04 20:38:38 -08:00

2.4 KiB

HttpRequest & HttpResponse

Response

Builder-like patter is used to construct an instance of HttpResponse. HttpResponse provides several method that returns HttpResponseBuilder instance, which is implements various convinience methods that helps build response. Check documentation for type description. Methods .body, .finish, .json finalizes response creation, if this methods get call for the same builder instance, builder will panic.

# extern crate actix_web;
use actix_web::*;

fn index(req: HttpRequest) -> HttpResponse {
    HttpResponse::Ok()
        .content_encoding(ContentEncoding::Br)
        .content_type("plain/text")
        .header("X-Hdr", "sample")
        .body("data").unwrap()
}
# fn main() {}

Content encoding

Actix automatically compress/decompress payload. Following codecs are supported:

  • Brotli
  • Gzip
  • Deflate
  • Identity

If request headers contains Content-Encoding header, request payload get decompressed according to header value. Multiple codecs are not supported, i.e: Content-Encoding: br, gzip.

Response payload get compressed based on content_encoding parameter. By default ContentEncoding::Auto is used. If ContentEncoding::Auto is selected then compression depends on request's Accept-Encoding header. ContentEncoding::Identity could be used to disable compression. If other content encoding is selected the compression is enforced for this codec. For example, to enable brotli response's body compression use ContentEncoding::Br:

# extern crate actix_web;
use actix_web::*;

fn index(req: HttpRequest) -> HttpResponse {
    HttpResponse::Ok()
        .content_encoding(ContentEncoding::Br)
        .body("data").unwrap()
}
# fn main() {}

JSON Response

The Json type allows you to respond with well-formed JSON data: simply return a value of type Json where T is the type of a structure to serialize into JSON. The type T must implement the Serialize trait from serde.

# extern crate actix_web;
#[macro_use] extern crate serde_derive;
use actix_web::*;

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

fn index(req: HttpRequest) -> Result<Json<MyObj>> {
    Ok(Json(MyObj{name: req.match_info().query("name")?}))
}

fn main() {
    Application::default("/")
        .resource(r"/a/{name}", |r| r.method(Method::GET).f(index))
        .finish();
}