Function reqwest_cross::fetch

source ·
pub fn fetch(
    request: RequestBuilder,
    on_done: impl 'static + Send + FnOnce(Result<Response, Error>)
)
Expand description

Performs a HTTP requests and calls the given callback when done. NB: Needs to use a callback to prevent blocking on the thread that initiates the fetch. Note: Instead of calling get like in the example you can use post, put, etc. (See reqwest::Client).

§Tokio example


 let request = Client::new().get("http://httpbin.org/get");
 let (tx, rx) = oneshot::channel();

 fetch(request, move |result: Result<Response, Error>| {
     tx.send(result.expect("Expecting Response not Error").status())
               .expect("Receiver should still be available");
 });

 let status = rx.await?; //In actual use case code to prevent blocking use try_recv instead
 assert_eq!(status, 200);