diff --git a/guide/src/qs_5.md b/guide/src/qs_5.md index 53573871c..a03bf91ee 100644 --- a/guide/src/qs_5.md +++ b/guide/src/qs_5.md @@ -11,7 +11,7 @@ Prefix handler: ```rust # extern crate actix_web; # use actix_web::*; - +# fn index(req: HttpRequest) -> HttpResponse { unimplemented!() } @@ -30,7 +30,7 @@ Application prefix combines with handler prefix i.e ```rust # extern crate actix_web; # use actix_web::*; - +# fn index(req: HttpRequest) -> HttpResponse { unimplemented!() } @@ -51,7 +51,7 @@ if no route could be matched default response `HTTPMethodNotAllowed` get resturn ```rust # extern crate actix_web; # use actix_web::*; - +# fn main() { Application::default("/") .resource("/prefix", |r| { @@ -102,7 +102,7 @@ You can also specify a custom regex in the form `{identifier:regex}`: # fn index(req: HttpRequest) -> String { # format!("Hello, {}", &req.match_info()["name"]) # } - +# fn main() { Application::default("/") .resource(r"{name:\d+}", |r| r.method(Method::GET).f(index)) @@ -115,7 +115,7 @@ implements `FromParam` trait. For example most of standard integer types implements `FromParam` trait. i.e.: ```rust -extern crate actix_web; +# extern crate actix_web; use actix_web::*; fn index(req: HttpRequest) -> Result { @@ -135,7 +135,13 @@ For this example for path '/a/1/2/', values v1 and v2 will resolve to "1" and "2 It is possible to match path tail with custom `.*` regex. -```rust,ignore +```rust +# extern crate actix_web; +# use actix_web::*; +# +# fn index(req: HttpRequest) -> HttpResponse { +# unimplemented!() +# } fn main() { Application::default("/") .resource(r"/test/{tail:.*}", |r| r.method(Method::GET).f(index)) diff --git a/guide/src/qs_7.md b/guide/src/qs_7.md index 5e1901c57..7668f0fef 100644 --- a/guide/src/qs_7.md +++ b/guide/src/qs_7.md @@ -1,9 +1,31 @@ # 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](../actix_web/dev/struct.HttpResponseBuilder.html) +for type description. Methods `.body`, `.finish`, `.json` finalizes response creation, +if this methods get call for the same builder instance, builder will panic. + +```rust +# 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: +Actix automatically *compress*/*decompress* payload. Following codecs are supported: * Brotli * Gzip diff --git a/src/httpresponse.rs b/src/httpresponse.rs index 4a447abeb..1bda6f186 100644 --- a/src/httpresponse.rs +++ b/src/httpresponse.rs @@ -373,6 +373,7 @@ impl HttpResponseBuilder { self } + /// Calls provided closure with builder reference if value is true. pub fn if_true(&mut self, value: bool, f: F) -> &mut Self where F: Fn(&mut HttpResponseBuilder) + 'static {