mirror of
https://github.com/actix/actix-website
synced 2024-12-03 20:22:13 +01:00
Update requests
This commit is contained in:
parent
b0e8a8d51d
commit
4f2f930349
@ -78,10 +78,10 @@ In the following example, we read and print the request payload chunk by chunk:
|
||||
{{< include-example example="requests" file="streaming.rs" section="streaming" >}}
|
||||
|
||||
[examples]: https://github.com/actix/examples/tree/master/json/
|
||||
[multipartstruct]: https://docs.rs/actix-multipart/0.1.2/actix_multipart/struct.Multipart.html
|
||||
[fieldstruct]: https://docs.rs/actix-multipart/0.1.2/actix_multipart/struct.Field.html
|
||||
[multipartstruct]: https://docs.rs/actix-multipart/0.2/actix_multipart/struct.Multipart.html
|
||||
[fieldstruct]: https://docs.rs/actix-multipart/0.2/actix_multipart/struct.Field.html
|
||||
[multipartexample]: https://github.com/actix/examples/tree/master/multipart/
|
||||
[urlencoded]: https://docs.rs/actix-web/1.0.2/actix_web/dev/struct.UrlEncoded.html
|
||||
[payloadextractor]: https://docs.rs/actix-web/1.0.2/actix_web/web/struct.Payload.html
|
||||
[urlencoded]: https://docs.rs/actix-web/2/actix_web/dev/struct.UrlEncoded.html
|
||||
[payloadextractor]: https://docs.rs/actix-web/2/actix_web/web/struct.Payload.html
|
||||
[multipartcrate]: https://crates.io/crates/actix-multipart
|
||||
[formencoded]:Jhttps://docs.rs/actix-web/1.0.2/actix_web/web/struct.Form.html
|
||||
[formencoded]:Jhttps://docs.rs/actix-web/2/actix_web/web/struct.Form.html
|
||||
|
@ -6,7 +6,8 @@ edition = "2018"
|
||||
[dependencies]
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
actix-web = "1.0"
|
||||
futures = "0.1"
|
||||
actix-web = "2.0"
|
||||
actix-rt = "1.0"
|
||||
futures = "0.3.1"
|
||||
bytes = "0.4"
|
||||
actix-multipart = "0.1"
|
||||
actix-multipart = "0.2"
|
||||
|
@ -14,15 +14,15 @@ struct Info {
|
||||
}
|
||||
|
||||
/// extract `Info` using serde
|
||||
fn index(info: web::Json<Info>) -> Result<String> {
|
||||
async fn index(info: web::Json<Info>) -> Result<String> {
|
||||
Ok(format!("Welcome {}!", info.username))
|
||||
}
|
||||
|
||||
fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
// </json-request>
|
||||
|
@ -1,7 +1,7 @@
|
||||
// <json-manual>
|
||||
use actix_web::{error, web, App, Error, HttpResponse};
|
||||
use bytes::BytesMut;
|
||||
use futures::{Future, Stream};
|
||||
use futures::StreamExt;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
|
||||
@ -13,41 +13,30 @@ struct MyObj {
|
||||
|
||||
const MAX_SIZE: usize = 262_144; // max payload size is 256k
|
||||
|
||||
pub fn index_manual(
|
||||
payload: web::Payload,
|
||||
) -> impl Future<Item = HttpResponse, Error = Error> {
|
||||
async fn index_manual(mut payload: web::Payload) -> Result<HttpResponse, Error> {
|
||||
// payload is a stream of Bytes objects
|
||||
payload
|
||||
// `Future::from_err` acts like `?` in that it coerces the error type from
|
||||
// the future into the final error type
|
||||
.from_err()
|
||||
// `fold` will asynchronously read each chunk of the request body and
|
||||
// call supplied closure, then it resolves to result of closure
|
||||
.fold(BytesMut::new(), move |mut body, chunk| {
|
||||
// limit max size of in-memory payload
|
||||
if (body.len() + chunk.len()) > MAX_SIZE {
|
||||
Err(error::ErrorBadRequest("overflow"))
|
||||
} else {
|
||||
body.extend_from_slice(&chunk);
|
||||
Ok(body)
|
||||
}
|
||||
})
|
||||
// `Future::and_then` can be used to merge an asynchronous workflow with a
|
||||
// synchronous workflow
|
||||
.and_then(|body| {
|
||||
// body is loaded, now we can deserialize serde-json
|
||||
let obj = serde_json::from_slice::<MyObj>(&body)?;
|
||||
Ok(HttpResponse::Ok().json(obj)) // <- send response
|
||||
})
|
||||
let mut body = BytesMut::new();
|
||||
while let Some(chunk) = payload.next().await {
|
||||
let chunk = chunk?;
|
||||
// limit max size of in-memory payload
|
||||
if (body.len() + chunk.len()) > MAX_SIZE {
|
||||
return Err(error::ErrorBadRequest("overflow"));
|
||||
}
|
||||
body.extend_from_slice(&chunk);
|
||||
}
|
||||
|
||||
// body is loaded, now we can deserialize serde-json
|
||||
let obj = serde_json::from_slice::<MyObj>(&body)?;
|
||||
Ok(HttpResponse::Ok().json(obj)) // <- send response
|
||||
}
|
||||
// </json-manual>
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::HttpServer;
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to_async(index_manual)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index_manual)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
@ -1,26 +1,24 @@
|
||||
// <streaming>
|
||||
use actix_web::{error, web, Error, HttpResponse};
|
||||
use futures::{future::result, Future, Stream};
|
||||
use futures::{Future, Stream, StreamExt};
|
||||
|
||||
pub fn index(payload: web::Payload) -> Box<dyn Future<Item = HttpResponse, Error = Error>> {
|
||||
Box::new(
|
||||
payload
|
||||
.from_err()
|
||||
.fold((), |_, chunk| {
|
||||
println!("Chunk: {:?}", chunk);
|
||||
result::<_, error::PayloadError>(Ok(()))
|
||||
})
|
||||
.map(|_| HttpResponse::Ok().into()),
|
||||
)
|
||||
async fn index(mut body: web::Payload) -> Result<HttpResponse, Error> {
|
||||
let mut bytes = web::BytesMut::new();
|
||||
while let Some(item) = body.next().await {
|
||||
bytes.extend_from_slice(&item?);
|
||||
}
|
||||
|
||||
println!("Chunk: {:?}", bytes);
|
||||
Ok(HttpResponse::Ok().finish())
|
||||
}
|
||||
// </streaming>
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to_async(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
@ -7,17 +7,17 @@ struct FormData {
|
||||
username: String,
|
||||
}
|
||||
|
||||
fn index(form: web::Form<FormData>) -> HttpResponse {
|
||||
async fn index(form: web::Form<FormData>) -> HttpResponse {
|
||||
HttpResponse::Ok().body(format!("username: {}", form.username))
|
||||
}
|
||||
// </urlencoded>
|
||||
|
||||
pub fn main() {
|
||||
#[actix_rt::main]
|
||||
async fn main() -> std::io::Result<()> {
|
||||
use actix_web::{App, HttpServer};
|
||||
|
||||
HttpServer::new(|| App::new().route("/", web::post().to(index)))
|
||||
.bind("127.0.0.1:8088")
|
||||
.unwrap()
|
||||
.bind("127.0.0.1:8088")?
|
||||
.run()
|
||||
.unwrap();
|
||||
.await
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user