1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-24 07:53:00 +01:00

update guide

This commit is contained in:
Nikolay Kim 2017-12-04 20:38:38 -08:00
parent fd6b243cd6
commit 3c9b6ea619
3 changed files with 37 additions and 8 deletions

View File

@ -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<String> {
@ -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))

View File

@ -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

View File

@ -373,6 +373,7 @@ impl HttpResponseBuilder {
self
}
/// Calls provided closure with builder reference if value is true.
pub fn if_true<F>(&mut self, value: bool, f: F) -> &mut Self
where F: Fn(&mut HttpResponseBuilder) + 'static
{