ehttp/lib.rs
1//! Minimal HTTP client for both native and WASM.
2//!
3//! Example:
4//! ```
5//! let request = ehttp::Request::get("https://www.example.com");
6//! ehttp::fetch(request, move |result: ehttp::Result<ehttp::Response>| {
7//! println!("Status code: {:?}", result.unwrap().status);
8//! });
9//! ```
10//!
11//! The given callback is called when the request is completed.
12//! You can communicate the results back to the main thread using something like:
13//!
14//! * Channels (e.g. [`std::sync::mpsc::channel`](https://doc.rust-lang.org/std/sync/mpsc/fn.channel.html)).
15//! * `Arc<Mutex<_>>`
16//! * [`poll_promise::Promise`](https://docs.rs/poll-promise)
17//! * [`eventuals::Eventual`](https://docs.rs/eventuals/latest/eventuals/struct.Eventual.html)
18//! * [`tokio::sync::watch::channel`](https://docs.rs/tokio/latest/tokio/sync/watch/fn.channel.html)
19//!
20//! ## Feature flags
21#![doc = document_features::document_features!()]
22//!
23
24/// Performs an HTTP request and calls the given callback when done.
25///
26/// `Ok` is returned if we get a response, even if it's a 404.
27///
28/// `Err` can happen for a number of reasons:
29/// * No internet connection
30/// * Connection timed out
31/// * DNS resolution failed
32/// * Firewall or proxy blocked the request
33/// * Server is not reachable
34/// * The URL is invalid
35/// * Server's SSL cert is invalid
36/// * CORS errors
37/// * The initial GET which returned HTML contained CSP headers to block access to the resource
38/// * A browser extension blocked the request (e.g. ad blocker)
39/// * …
40pub fn fetch(request: Request, on_done: impl 'static + Send + FnOnce(Result<Response>)) {
41 #[cfg(not(target_arch = "wasm32"))]
42 native::fetch(request, Box::new(on_done));
43
44 #[cfg(target_arch = "wasm32")]
45 web::fetch(request, Box::new(on_done));
46}
47
48/// Performs an `async` HTTP request.
49///
50/// Available on following platforms:
51/// - web
52/// - native behind the `native-async` feature.
53///
54/// `Ok` is returned if we get a response, even if it's a 404.
55///
56/// `Err` can happen for a number of reasons:
57/// * No internet connection
58/// * Connection timed out
59/// * DNS resolution failed
60/// * Firewall or proxy blocked the request
61/// * Server is not reachable
62/// * The URL is invalid
63/// * Server's SSL cert is invalid
64/// * CORS errors
65/// * The initial GET which returned HTML contained CSP headers to block access to the resource
66/// * A browser extension blocked the request (e.g. ad blocker)
67/// * …
68#[cfg(any(target_arch = "wasm32", feature = "native-async"))]
69pub async fn fetch_async(request: Request) -> Result<Response> {
70 #[cfg(not(target_arch = "wasm32"))]
71 return native::fetch_async(request).await;
72
73 #[cfg(target_arch = "wasm32")]
74 return web::fetch_async(&request).await;
75}
76
77mod types;
78pub use types::{Error, Headers, PartialResponse, Request, Response, Result};
79
80#[cfg(target_arch = "wasm32")]
81pub use types::Mode;
82
83#[cfg(not(target_arch = "wasm32"))]
84mod native;
85#[cfg(not(target_arch = "wasm32"))]
86pub use native::fetch_blocking;
87
88#[cfg(target_arch = "wasm32")]
89mod web;
90#[cfg(target_arch = "wasm32")]
91pub use web::spawn_future;
92
93#[cfg(feature = "streaming")]
94pub mod streaming;
95
96#[cfg(feature = "multipart")]
97pub mod multipart;
98
99#[deprecated = "Use ehttp::Headers::new"]
100pub fn headers(headers: &[(&str, &str)]) -> Headers {
101 Headers::new(headers)
102}