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