mirror of
https://github.com/actix/actix-website
synced 2025-02-08 22:36:07 +01:00
Responses section is done-ish.
This commit is contained in:
parent
d7d4392d87
commit
1c3697197d
@ -6,15 +6,15 @@ weight: 210
|
|||||||
|
|
||||||
# Response
|
# Response
|
||||||
|
|
||||||
A builder-like pattern is used to construct an instance of `HttpResponse`.
|
A builder-like pattern is used to construct an instance of `HttpResponse`. `HttpResponse`
|
||||||
`HttpResponse` provides several methods that return a `HttpResponseBuilder` instance,
|
provides several methods that return a `HttpResponseBuilder` instance, which implements
|
||||||
which implements various convenience methods for building responses.
|
various convenience methods for building responses.
|
||||||
|
|
||||||
> Check the [documentation][responsebuilder] for type descriptions.
|
> Check the [documentation][responsebuilder] for type descriptions.
|
||||||
|
|
||||||
The methods `.body`, `.finish`, and `.json` finalize response creation and
|
The methods `.body`, `.finish`, and `.json` finalize response creation and return a
|
||||||
return a constructed *HttpResponse* instance. If this methods is called on the same
|
constructed *HttpResponse* instance. If this methods is called on the same builder
|
||||||
builder instance multiple times, the builder will panic.
|
instance multiple times, the builder will panic.
|
||||||
|
|
||||||
{{< include-example example="responses" file="main.rs" section="builder" >}}
|
{{< include-example example="responses" file="main.rs" section="builder" >}}
|
||||||
|
|
||||||
@ -39,8 +39,8 @@ For example, to enable `brotli` use `ContentEncoding::Br`:
|
|||||||
|
|
||||||
{{< include-example example="responses" file="brotli.rs" section="brotli" >}}
|
{{< include-example example="responses" file="brotli.rs" section="brotli" >}}
|
||||||
|
|
||||||
In this case we explicitly disable content compression
|
In this case we explicitly disable content compression by setting content encoding to
|
||||||
by setting content encoding to a `Identity` value:
|
an `Identity` value:
|
||||||
|
|
||||||
{{< include-example example="responses" file="identity.rs" section="identity" >}}
|
{{< include-example example="responses" file="identity.rs" section="identity" >}}
|
||||||
|
|
||||||
|
@ -1,16 +1,22 @@
|
|||||||
// <auto>
|
// <auto>
|
||||||
use actix_web::{
|
use actix_web::{http::ContentEncoding, middleware, HttpResponse};
|
||||||
http::ContentEncoding, middleware, web, App, HttpRequest, HttpResponse,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> HttpResponse {
|
fn index() -> HttpResponse {
|
||||||
HttpResponse::Ok().body("data")
|
HttpResponse::Ok().body("data")
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
App::new()
|
use actix_web::{web, App, HttpServer};
|
||||||
// v- disable compression for all routes
|
|
||||||
.wrap(middleware::Compress::new(ContentEncoding::Identity))
|
HttpServer::new(|| {
|
||||||
.route("/", web::get().to(index));
|
App::new()
|
||||||
|
// v- disable compression for all routes
|
||||||
|
.wrap(middleware::Compress::new(ContentEncoding::Identity))
|
||||||
|
.route("/", web::get().to(index))
|
||||||
|
})
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
// </auto>
|
// </auto>
|
||||||
|
@ -1,16 +1,19 @@
|
|||||||
// <brotli>
|
// <brotli>
|
||||||
use actix_web::{
|
use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse};
|
||||||
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn index_br(_req: HttpRequest) -> HttpResponse {
|
fn index_br() -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.encoding(ContentEncoding::Br)
|
.encoding(ContentEncoding::Br)
|
||||||
.body("data")
|
.body("data")
|
||||||
}
|
}
|
||||||
// </brotli>
|
// </brotli>
|
||||||
|
|
||||||
use actix_web::{web, App};
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
App::new().route("/", web::get().to(index_br));
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
|
HttpServer::new(|| App::new().route("/", web::get().to(index_br)))
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -11,4 +11,13 @@
|
|||||||
// ))))))
|
// ))))))
|
||||||
// }
|
// }
|
||||||
// </chunked>
|
// </chunked>
|
||||||
pub fn main() {}
|
|
||||||
|
// pub fn main() {
|
||||||
|
// use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
|
// HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
|
// .bind("127.0.0.1:8088")
|
||||||
|
// .unwrap()
|
||||||
|
// .run()
|
||||||
|
// .unwrap();
|
||||||
|
// }
|
||||||
|
@ -1,9 +1,7 @@
|
|||||||
// <identity>
|
// <identity>
|
||||||
use actix_web::{
|
use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse};
|
||||||
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> HttpResponse {
|
fn index() -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
// v- disable compression
|
// v- disable compression
|
||||||
.encoding(ContentEncoding::Identity)
|
.encoding(ContentEncoding::Identity)
|
||||||
@ -11,7 +9,12 @@ fn index(_req: HttpRequest) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
// </identity>
|
// </identity>
|
||||||
|
|
||||||
use actix_web::{web, App};
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
App::new().route("/", web::get().to(index));
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -17,7 +17,12 @@ pub fn index(_req: HttpRequest) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
// </identity-two>
|
// </identity-two>
|
||||||
|
|
||||||
use actix_web::{web, App};
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
App::new().route("/", web::get().to(index));
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
@ -1,19 +1,25 @@
|
|||||||
// <json-resp>
|
// <json-resp>
|
||||||
use actix_web::{web, App, HttpRequest, Result};
|
use actix_web::{web, HttpResponse, Result};
|
||||||
use serde::Serialize;
|
use serde::{Deserialize, Serialize};
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize, Deserialize)]
|
||||||
struct MyObj {
|
struct MyObj {
|
||||||
name: String,
|
name: String,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn index(req: HttpRequest) -> Result<web::Json<MyObj>> {
|
fn index(obj: web::Path<MyObj>) -> Result<HttpResponse> {
|
||||||
Ok(web::Json(MyObj {
|
Ok(HttpResponse::Ok().json(MyObj {
|
||||||
name: req.match_info().query("name").to_string(),
|
name: obj.name.to_string(),
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
App::new().route(r"/a/{name}", web::get().to(index));
|
use actix_web::{App, HttpServer};
|
||||||
|
|
||||||
|
HttpServer::new(|| App::new().route(r"/a/{name}", web::get().to(index)))
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
// </json-resp>
|
// </json-resp>
|
||||||
|
@ -4,12 +4,11 @@ pub mod chunked;
|
|||||||
pub mod identity;
|
pub mod identity;
|
||||||
pub mod identity_two;
|
pub mod identity_two;
|
||||||
pub mod json_resp;
|
pub mod json_resp;
|
||||||
// <builder>
|
|
||||||
use actix_web::{
|
|
||||||
http::ContentEncoding, middleware::BodyEncoding, HttpRequest, HttpResponse,
|
|
||||||
};
|
|
||||||
|
|
||||||
fn index(_req: HttpRequest) -> HttpResponse {
|
// <builder>
|
||||||
|
use actix_web::{http::ContentEncoding, middleware::BodyEncoding, HttpResponse};
|
||||||
|
|
||||||
|
fn index() -> HttpResponse {
|
||||||
HttpResponse::Ok()
|
HttpResponse::Ok()
|
||||||
.encoding(ContentEncoding::Br)
|
.encoding(ContentEncoding::Br)
|
||||||
.content_type("plain/text")
|
.content_type("plain/text")
|
||||||
@ -18,7 +17,12 @@ fn index(_req: HttpRequest) -> HttpResponse {
|
|||||||
}
|
}
|
||||||
// </builder>
|
// </builder>
|
||||||
|
|
||||||
use actix_web::{web, App};
|
|
||||||
pub fn main() {
|
pub fn main() {
|
||||||
App::new().route("/", web::get().to(index));
|
use actix_web::{web, App, HttpServer};
|
||||||
|
|
||||||
|
HttpServer::new(|| App::new().route("/", web::get().to(index)))
|
||||||
|
.bind("127.0.0.1:8088")
|
||||||
|
.unwrap()
|
||||||
|
.run()
|
||||||
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user