您链接到的具体示例在使用 async的reqwest
crate之前。如果你想使用那个确切的例子,那么reqwest::Client
你需要使用. 而不是reqwest::blocking::Client
. 这也需要启用该blocking
功能。
需要明确的是,您实际上仍然可以找到该示例,它只是位于reqwest::blocking::RequestBuilder
'sbody()
方法的文档中。
// reqwest = { version = "0.11", features = ["blocking"] }
use reqwest::blocking::Client;
use std::fs::File;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open("from_a_file.txt")?;
let client = Client::new();
let res = client.post("http://httpbin.org/post")
.body(file)
.send()?;
Ok(())
}
还要检查reqwest
'sForm
和RequestBuilder
'multipart()
方法,例如有一个file()
方法。
如果您确实想使用异步,那么您可以使用FramedRead
from the tokio-util
crate。连同TryStreamExt
特质,来自futures
crate。
只需确保启用 的stream
功能reqwest
和 的codec
功能tokio-util
。
// futures = "0.3"
use futures::stream::TryStreamExt;
// reqwest = { version = "0.11", features = ["stream"] }
use reqwest::{Body, Client};
// tokio = { version = "1.0", features = ["full"] }
use tokio::fs::File;
// tokio-util = { version = "0.6", features = ["codec"] }
use tokio_util::codec::{BytesCodec, FramedRead};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open("from_a_file.txt").await?;
let client = reqwest::Client::new();
let res = client
.post("http://httpbin.org/post")
.body(file_to_body(file))
.send()
.await?;
Ok(())
}
fn file_to_body(file: File) -> Body {
let stream = FramedRead::new(file, BytesCodec::new());
let body = Body::wrap_stream(stream);
body
}