【问题标题】:How do I convert a reqwest stream into an actix stream?如何将 reqwest 流转换为 actix 流?
【发布时间】:2020-04-06 21:20:13
【问题描述】:

我正在尝试将来自 reqwest 的流转换为 actix 响应的流,以避免在内存中创建大的 Vec

use actix_web::{web, HttpRequest, HttpResponse, Responder};

pub async fn test(
    req: HttpRequest
) -> impl Responder {
    let request = reqwest::get("https://www.rust-lang.org").await;
    if request.is_err() {
        println!("Error: {:?}", request.err());
    }
    let request = request.unwrap();
    let stream = request.bytes_stream();

    HttpResponse::Ok()
        .content_type("text/html")
        .streaming(stream)
}

编译器给了我:

  --> src/routes/test.rs:97:10
   |
97 |         .streaming(stream)
   |          ^^^^^^^^^ the trait `actix_http::error::ResponseError` is not implemented for `reqwest::error::Error`
   |
   = note: required because of the requirements on the impl of `std::convert::From<reqwest::error::Error>` for `actix_http::error::Error`
   = note: required because of the requirements on the impl of `std::convert::Into<actix_http::error::Error>` for `reqwest::error::Error`

我已经尽力了,但没有运气:

  • 创建一个传递Stream(连编译都不行,我不擅长转换类型)
  • 实现 Reqwest 错误的缺失错误(该错误未公开,因此我无法这样做)
  • 无论如何都在尝试(但不确定我是否使用正确)

【问题讨论】:

标签: rust rust-tokio


【解决方案1】:

我写了一个薄层来连接两个系统,传递错误类型: https://github.com/clia/reqwest-actix-stream

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2011-08-12
    • 1970-01-01
    • 2017-01-31
    • 2015-10-19
    相关资源
    最近更新 更多