我正在学习 Rust 并尝试通过发送一些 POST 数据来爬取一个随机网站,但是我遇到了一堆错误信息,如下所示:
以下是代码。我已经多次查看文档,但我返回的是 Result<>,所以不确定哪里有问题。
error[E0277]: the `?` operator can only be used in an async block that returns `Result` or `Option` (or another type that implements `FromResidual`)
37 | | let text = res.text().await?;
| | ^ cannot use the `?` operator in an async block that returns `()`
以下是代码。我已经多次查看文档,但我返回的是 Result<>,所以不确定哪里有问题。
use std::sync::Arc;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
println!("Hello, world!");
use reqwest::{cookie::Jar, Url};
use reqwest::header;
let mut headers = header::HeaderMap::new();
headers.insert("User-Agent", header::HeaderValue::from_static("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/94.0.4606.81 Safari/537.36"));
headers.insert("Content-Type", header::HeaderValue::from_static("application/json"));
let cookie = "cookies";
let jar = Jar::default();
let cookie_url = "https://url.com/".parse::<Url>()?;
jar.add_cookie_str(cookie, &cookie_url);
let body = String::from("post body");
let client = reqwest::Client::builder()
.default_headers(headers)
.cookie_store(true)
.cookie_provider(Arc::new(jar))
.build()?;
let path = "https://long_url.com".parse::<Url>()?;
let res = client
.post(path)
.body(body)
.send()
.await?;
let text = res.text().await?;
println!("{}", text);
Ok(());
}
谢谢!
main
函数末尾有一个多余的分号。 - Jmb