reqwest_cross

Function fetch

Source
pub fn fetch<F, O>(request: RequestBuilder, on_done: F)
where F: DoneHandler<O>, O: BoundedFuture<()>,
Expand description

Performs a HTTP requests and calls the given callback when done with the result of the request. This is a more flexible API but requires more boilerplate, see fetch_plus which wraps a lot more of the boilerplate especially if you need a “wake_up” function. 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.

§Example


 let client = reqwest::Client::new();
 let request = client.get("https://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);