如何使用 reqwest 发布文件? [英] How to POST a file using reqwest?

查看:52
本文介绍了如何使用 reqwest 发布文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

reqwest v0.9.18 的文档

The documentation for reqwest v0.9.18 shows the following example of posting a file:

let file = fs::File::open("from_a_file.txt")?;
let client = reqwest::Client::new();
let res = client.post("http://httpbin.org/post")
    .body(file)
    .send()?;

reqwest v0.11的最新文档 不再包含此示例,并且在调用 body() 时尝试构建它失败并出现以下错误:

The latest documentation for reqwest v0.11 no longer includes this example, and trying to build it fails with the following error when calling body():

the trait `From<std::fs::File>` is not implemented for `Body`

发送文件的更新方法是什么?

What is the updated method for sending a file?

推荐答案

您要链接的具体示例是在 reqwest crate 使用异步.如果你想使用那个确切的例子,而不是 reqwest::Client,你需要使用reqwest::blocking::Client.这还需要启用 blocking 功能.

The specific example you're linking to, was prior to the reqwest crate using async. If you want to use that exact example, then instead of reqwest::Client, you need to use reqwest::blocking::Client. This also requires enabling the blocking feature.

需要明确的是,您实际上仍然可以找到该示例,它位于 reqwest::blocking::RequestBuilderbody() 方法.

To be clear, you can actually still find that example, it's just located in the docs for reqwest::blocking::RequestBuilder's body() method instead.

// 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(())
}

另请查看 reqwestFormRequestBuildermultipart() 方法,例如有一个 file() 方法.

Also check out reqwest's Form and RequestBuilder's multipart() method, as there for instance is a file() method.

如果您确实想使用异步,那么您可以使用 FramedRead 来自 tokio-util 箱子.随着TryStreamExt trait,来自 futures crate.

If you do want to use async, then you can use FramedRead from the tokio-util crate. Along with the TryStreamExt trait, from the futures crate.

只需确保为 reqwest 启用 stream 功能,为 tokio-util 启用 codec 功能.

Just make sure to enable the stream feature for reqwest, and the codec feature for 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
}

这篇关于如何使用 reqwest 发布文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆