I'm learning rust and trying to scrape a random site by sending some POST data, and I'm getting a bunch of errors like:
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 `()`
The code is below. I've gone through the docs multiple times, but I'm returning a Result<> so I'm not sure what's wrong.
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(());
}
Thank you!