Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add big_file example. #888

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,8 @@ serde = { version = "1.0", features = ["derive"] }
libflate = "0.1"
brotli_crate = { package = "brotli", version = "3.3.0" }
doc-comment = "0.3"
tokio = { version = "0.2.0", default-features = false, features = ["macros"] }
tokio = { version = "0.2.0", default-features = false, features = ["fs", "io-std", "macros"] }
tokio-util = { version = "0.3", features = ["codec"] }

[target.'cfg(windows)'.dependencies]
winreg = "0.6"
Expand Down Expand Up @@ -150,6 +151,11 @@ name = "blocking"
path = "examples/blocking.rs"
required-features = ["blocking"]

[[example]]
name = "big_file"
path = "examples/big_file.rs"
required-features = ["stream"]

[[example]]
name = "json_dynamic"
path = "examples/json_dynamic.rs"
Expand Down
5 changes: 5 additions & 0 deletions examples/big_file.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"user_id": 1,
"title": "very long title",
"body": "very long body"
}
44 changes: 44 additions & 0 deletions examples/big_file.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//! `cargo run --example big_file --features=stream`
//! This example illustrates the way to send and receive big file by chunks.
#![deny(warnings)]

use reqwest::{
header::{HeaderMap, HeaderValue, CONTENT_TYPE},
Body,
};
use tokio::{
fs::File,
io::{self, AsyncWriteExt},
stream::StreamExt,
};
use tokio_util::codec::{BytesCodec, FramedRead};

static FILE_PATH: &str = "examples/big_file.json";

// This is using the `tokio` runtime. You'll need the following dependency:
//
// `tokio = { version = "0.2", features = ["fs", "io-std", "macros"] }`
//
// for reading file by chunks this example uses tokio-util dependency:
//
// `tokio-util = { version = "0.3", features = ["codec"] }`
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open(FILE_PATH).await?;
let stream = FramedRead::new(file, BytesCodec::new());
let body = Body::wrap_stream(stream);
let mut headers = HeaderMap::new();
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
let mut response_stream = reqwest::Client::new()
.post("https://jsonplaceholder.typicode.com/posts")
.headers(headers)
.body(body)
.send()
.await?
.bytes_stream();
while let Some(chunk) = response_stream.next().await {
let chunk = chunk?;
io::stdout().write_all(&chunk).await?;
}
Ok(())
}