Function reqwest_cross::fetch

source ·
pub fn fetch<F, O>(request: RequestBuilder, on_done: F)
where F: 'static + Send + FnOnce(Result<Response>) -> O, O: Future<Output = ()> + Send,
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). Also see the examples folder for more complete examples.

§Tokio example


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

 fetch(request, move |result: Result<reqwest::Response, reqwest::Error>| async {
     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);