gpt4 book ai didi

asynchronous - 如何在 Rust Hyper 中将响应主体读取为字符串?

转载 作者:行者123 更新时间:2023-12-03 11:45:03 26 4
gpt4 key购买 nike

这个问题有几个答案( hereherehere ),但没有一个对我有用:(

到目前为止我尝试了什么:


use hyper as http;
use futures::TryStreamExt;

fn test_heartbeat() {
let mut runtime = tokio::runtime::Runtime::new().expect("Could not get default runtime");

runtime.spawn(httpserve());

let addr = "http://localhost:3030".parse().unwrap();
let expected = json::to_string(&HeartBeat::default()).unwrap();

let client = http::Client::new();
let actual = runtime.block_on(client.get(addr));

assert!(actual.is_ok());

if let Ok(response) = actual {
let (_, body) = response.into_parts();

// what shall be done here?
}
}

我不确定,这里要做什么?

最佳答案

这对我有用(使用 hyper 0.2.1):

async fn body_to_string(req: Request<Body>) -> String {
let body_bytes = hyper::body::to_bytes(req.into_body()).await?;
String::from_utf8(body_bytes.to_vec()).unwrap()
}

关于asynchronous - 如何在 Rust Hyper 中将响应主体读取为字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63301838/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com