1
0
mirror of https://github.com/actix/actix-website synced 2024-11-27 18:12:57 +01:00

Requests is done-ish.

This commit is contained in:
Cameron Dershem 2019-06-26 01:27:17 -04:00
parent be68e418b0
commit d7d4392d87
6 changed files with 69 additions and 47 deletions

View File

@ -6,7 +6,7 @@ weight: 200
# Content Encoding # Content Encoding
Actix automatically *decompresses* payloads. The following codecs are supported: Actix-web automatically *decompresses* payloads. The following codecs are supported:
* Brotli * Brotli
* Chunked * Chunked
@ -42,29 +42,24 @@ body first and then deserialize the json into an object.
# Chunked transfer encoding # Chunked transfer encoding
Actix automatically decodes *chunked* encoding. `HttpRequest::payload()` already contains Actix automatically decodes *chunked* encoding. The [`web::Payload`][payloadextractor]
the decoded byte stream. If the request payload is compressed with one of the supported extractor already contains the decoded byte stream. If the request payload is compressed
compression codecs (br, gzip, deflate), then the byte stream is decompressed. with one of the supported compression codecs (br, gzip, deflate), then the byte stream
is decompressed.
# Multipart body # Multipart body
Actix provides multipart stream support. Actix provides multipart stream support with an external crate, [`actix-multipart`][multipartcrate].
[*Multipart*][multipartstruct] is implemented as a stream of multipart items. Each item
can be a [*Field*][fieldstruct] or a nested *Multipart* stream.`HttpResponse::multipart()`
returns the *Multipart* stream for the current request.
The following demonstrates multipart stream handling for a simple form: The following demonstrates multipart stream handling for a simple form:
{{< include-example example="requests" file="multipart.rs" section="multipart" >}}
> A full example is available in the [examples directory][multipartexample]. > A full example is available in the [examples directory][multipartexample].
# Urlencoded body # Urlencoded body
Actix provides support for *application/x-www-form-urlencoded* encoded bodies. Actix-web provides support for *application/x-www-form-urlencoded* encoded bodies with
`HttpResponse::urlencoded()` returns a [*UrlEncoded*][urlencoder] future, which resolves the [`web::Form`][urlencoded] extractor which resolves to the deserialized instance. The
to the deserialized instance. The type of the instance must implement the `Deserialize` type of the instance must implement the `Deserialize` trait from *serde*.
trait from *serde*.
The *UrlEncoded* future can resolve into an error in several cases: The *UrlEncoded* future can resolve into an error in several cases:
@ -89,3 +84,6 @@ In the following example, we read and print the request payload chunk by chunk:
[fieldstruct]: ../../actix-web/actix_web/multipart/struct.Field.html [fieldstruct]: ../../actix-web/actix_web/multipart/struct.Field.html
[multipartexample]: https://github.com/actix/examples/tree/master/multipart/ [multipartexample]: https://github.com/actix/examples/tree/master/multipart/
[urlencoder]: ../../actix-web/actix_web/dev/struct.UrlEncoded.html [urlencoder]: ../../actix-web/actix_web/dev/struct.UrlEncoded.html
[payloadextractor]: https://docs.rs/actix-web/1.0.2/actix_web/web/struct.Payload.html
[multipartcrate]: https://crates.io/crates/actix-multipart
[urlencoded]:Jhttps://docs.rs/actix-web/1.0.2/actix_web/web/struct.Form.html

View File

@ -9,3 +9,4 @@ serde_json = "1.0"
actix-web = "1.0" actix-web = "1.0"
futures = "0.1" futures = "0.1"
bytes = "0.4" bytes = "0.4"
actix-multipart = "0.1"

View File

@ -3,8 +3,9 @@ pub mod manual;
pub mod multipart; pub mod multipart;
pub mod streaming; pub mod streaming;
pub mod urlencoded; pub mod urlencoded;
// <json-request> // <json-request>
use actix_web::{web, App, Result}; use actix_web::{web, App, HttpServer, Result};
use serde::Deserialize; use serde::Deserialize;
#[derive(Deserialize)] #[derive(Deserialize)]
@ -18,6 +19,10 @@ fn index(info: web::Json<Info>) -> Result<String> {
} }
fn main() { fn main() {
App::new().route("/index.html", web::post().to(index)); HttpServer::new(|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
} }
// </json-request> // </json-request>

View File

@ -43,5 +43,11 @@ pub fn index_manual(
// </json-manual> // </json-manual>
pub fn main() { pub fn main() {
App::new().route("/", web::post().to_async(index_manual)); use actix_web::HttpServer;
HttpServer::new(|| App::new().route("/", web::post().to_async(index_manual)))
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
} }

View File

@ -1,15 +1,26 @@
// <streaming> // <streaming>
// use actix_web::{error, web, Error, HttpResponse}; use actix_web::{error, web, Error, HttpResponse};
// use futures::{future::result, Future, Stream}; use futures::{future::result, Future, Stream};
// pub fn index(payload: web::Payload) -> Box<Future<Item = HttpResponse, Error = Error>> { pub fn index(payload: web::Payload) -> Box<Future<Item = HttpResponse, Error = Error>> {
// payload Box::new(
// .from_err() payload
// .fold((), |_, chunk| { .from_err()
// println!("Chunk: {:?}", chunk); .fold((), |_, chunk| {
// result::<_, error::PayloadError>(Ok(())) println!("Chunk: {:?}", chunk);
// }) result::<_, error::PayloadError>(Ok(()))
// .map(|_| HttpResponse::Ok().finish()) })
// .responder() .map(|_| HttpResponse::Ok().into()),
// } )
}
// </streaming> // </streaming>
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().route("/", web::post().to_async(index)))
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
}

View File

@ -1,22 +1,23 @@
// <urlencoded> // <urlencoded>
// use actix_web::{Error, HttpRequest, HttpResponse}; use actix_web::{web, HttpResponse};
// use futures::future::{ok, Future}; use serde::Deserialize;
// use serde::Deserialize;
// #[derive(Deserialize)] #[derive(Deserialize)]
// struct FormData { struct FormData {
// username: String, username: String,
// } }
// fn index(req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> { fn index(form: web::Form<FormData>) -> HttpResponse {
// req.urlencoded::<FormData>() // <- get UrlEncoded future HttpResponse::Ok().body(format!("username: {}", form.username))
// .from_err() }
// .and_then(|data| {
// // <- deserialized instance
// println!("USERNAME: {:?}", data.username);
// ok(HttpResponse::Ok().into())
// })
// .responder()
// }
// </urlencoded> // </urlencoded>
pub fn main() {}
pub fn main() {
use actix_web::{App, HttpServer};
HttpServer::new(|| App::new().route("/", web::post().to(index)))
.bind("127.0.0.1:8088")
.unwrap()
.run()
.unwrap();
}