1
0
mirror of https://github.com/actix/actix-website synced 2024-11-24 00:41:07 +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
Actix automatically *decompresses* payloads. The following codecs are supported:
Actix-web automatically *decompresses* payloads. The following codecs are supported:
* Brotli
* Chunked
@ -42,29 +42,24 @@ body first and then deserialize the json into an object.
# Chunked transfer encoding
Actix automatically decodes *chunked* encoding. `HttpRequest::payload()` already contains
the decoded byte stream. If the request payload is compressed with one of the supported
compression codecs (br, gzip, deflate), then the byte stream is decompressed.
Actix automatically decodes *chunked* encoding. The [`web::Payload`][payloadextractor]
extractor already contains the decoded byte stream. If the request payload is compressed
with one of the supported compression codecs (br, gzip, deflate), then the byte stream
is decompressed.
# Multipart body
Actix provides multipart stream support.
[*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.
Actix provides multipart stream support with an external crate, [`actix-multipart`][multipartcrate].
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].
# Urlencoded body
Actix provides support for *application/x-www-form-urlencoded* encoded bodies.
`HttpResponse::urlencoded()` returns a [*UrlEncoded*][urlencoder] future, which resolves
to the deserialized instance. The type of the instance must implement the `Deserialize`
trait from *serde*.
Actix-web provides support for *application/x-www-form-urlencoded* encoded bodies with
the [`web::Form`][urlencoded] extractor which resolves to the deserialized instance. The
type of the instance must implement the `Deserialize` trait from *serde*.
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
[multipartexample]: https://github.com/actix/examples/tree/master/multipart/
[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"
futures = "0.1"
bytes = "0.4"
actix-multipart = "0.1"

View File

@ -3,8 +3,9 @@ pub mod manual;
pub mod multipart;
pub mod streaming;
pub mod urlencoded;
// <json-request>
use actix_web::{web, App, Result};
use actix_web::{web, App, HttpServer, Result};
use serde::Deserialize;
#[derive(Deserialize)]
@ -18,6 +19,10 @@ fn index(info: web::Json<Info>) -> Result<String> {
}
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>

View File

@ -43,5 +43,11 @@ pub fn index_manual(
// </json-manual>
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>
// use actix_web::{error, web, Error, HttpResponse};
// use futures::{future::result, Future, Stream};
use actix_web::{error, web, Error, HttpResponse};
use futures::{future::result, Future, Stream};
// pub fn index(payload: web::Payload) -> Box<Future<Item = HttpResponse, Error = Error>> {
// payload
// .from_err()
// .fold((), |_, chunk| {
// println!("Chunk: {:?}", chunk);
// result::<_, error::PayloadError>(Ok(()))
// })
// .map(|_| HttpResponse::Ok().finish())
// .responder()
// }
pub fn index(payload: web::Payload) -> Box<Future<Item = HttpResponse, Error = Error>> {
Box::new(
payload
.from_err()
.fold((), |_, chunk| {
println!("Chunk: {:?}", chunk);
result::<_, error::PayloadError>(Ok(()))
})
.map(|_| HttpResponse::Ok().into()),
)
}
// </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>
// use actix_web::{Error, HttpRequest, HttpResponse};
// use futures::future::{ok, Future};
// use serde::Deserialize;
use actix_web::{web, HttpResponse};
use serde::Deserialize;
// #[derive(Deserialize)]
// struct FormData {
// username: String,
// }
#[derive(Deserialize)]
struct FormData {
username: String,
}
// fn index(req: &HttpRequest) -> Box<Future<Item = HttpResponse, Error = Error>> {
// req.urlencoded::<FormData>() // <- get UrlEncoded future
// .from_err()
// .and_then(|data| {
// // <- deserialized instance
// println!("USERNAME: {:?}", data.username);
// ok(HttpResponse::Ok().into())
// })
// .responder()
// }
fn index(form: web::Form<FormData>) -> HttpResponse {
HttpResponse::Ok().body(format!("username: {}", form.username))
}
// </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();
}