I am writing a web service with Rust 2018 Stable and Actix-Web. Using Reqwest, I am making an HTTP request to a diffent site from within one route handler function. Simplyfied it looks like this
extern crate reqwest;
use actix_web;
use reqwest::Url;
pub fn testing(req: actix_web::HttpRequest) -> actix_web::Result<actix_web::HttpResponse> {
println!(">>> testing request begin");
let url = Url::parse("https://example.com/").unwrap();
println!(">>> testing url built");
let req = reqwest::Client::new().post(url);
println!(">>> testing req prepared");
let res_struct = req.send();
println!(">>> testing res_struct received");
let res = res_struct.unwrap();
println!(">>> testing res unwrapped");
Ok(format!("done.").into())
}
That doesn't work, and I am getting the following error message (the error is printed 8 times, "worker:1" to "worker:8", despite calling the function only once):
thread 'actix-rt:worker:1' panicked at 'called `Result::unwrap()`
on an `Err` value: Error(BlockingClientInFutureContext,
"https://www.example.com/")', src/libcore/result.rs:999:5
Panic in Arbiter thread, shutting down system.
Google didn't find anything useful on "BlockingClientInFutureContext", but I am guessing it is somehow related to async/await or maybe Tokio's own futures?
Thanks for any pointers about what to read up on. Also, I am new to Rust.
The handler function is called from the Actix-Web HTttpServer:
HttpServer::new(|| App::new().service(
web::resource("/testing").route(
web::get().to(views::testing)
)
)).bind("127.0.0.1:8001")?.run()
.post(Url::parse(API_ENDPOINT).unwrap())
, you are calling unwrap(), which will result in a panic if the unwrap() fails. Have you tried changing this to a call that does not use unwrap()? – Gardeneractix_web::web::block()
somehow. – C14L