[−][src]Crate surf
surf the web.
Surf is the Rust HTTP client we've always wanted. It's completely modular, and
built directly for async/await. Whether it's a quick script, or a
cross-platform SDK, Surf will make it work.
- Multi-platform out of the box
- Extensible through a powerful middleware system
- Reuses connections through the
Clientinterface - Fully streaming requests and responses
- TLS/SSL enabled by default
- Swappable HTTP backends
- HTTP/2 enabled by default
Examples
let mut res = surf::get("https://httpbin.org/get").await?; dbg!(res.body_string().await?);
It's also possible to skip the intermediate Response, and access the response type directly.
dbg!(surf::get("https://httpbin.org/get").recv_string().await?);
Both sending and receiving JSON is real easy too.
#[derive(Deserialize, Serialize)] struct Ip { ip: String } let uri = "https://httpbin.org/post"; let data = &Ip { ip: "129.0.0.1".into() }; let res = surf::post(uri).body_json(data)?.await?; assert_eq!(res.status(), 200); let uri = "https://api.ipify.org?format=json"; let Ip { ip } = surf::get(uri).recv_json().await?; assert!(ip.len() > 10);
And even creating streaming proxies is no trouble at all.
let reader = surf::get("https://img.fyi/q6YvNqP").await?; let res = surf::post("https://box.rs/upload").body(reader).await?;
Features
The following features are available.
native-client(default): usecurlon the server andwindow.fetchin the browser.middleware-logger(default): enables logging requests and responses using a middleware.curl-client: usecurl(throughisahc) as the HTTP backend.hyper-client: usehyperas the HTTP backend.wasm-client: usewindow.fetchas the HTTP backend.
Re-exports
pub use http; |
pub use mime; |
pub use url; |
Modules
| headers | HTTP Headers. |
| middleware | Middleware types |
Structs
| Client | An HTTP client, capable of creating new |
| Request | An HTTP request, returns a |
| Response | An HTTP response, returned by |
Functions
| connect | Perform a one-off |
| delete | Perform a one-off |
| get | Perform a one-off |
| head | Perform a one-off |
| options | Perform a one-off |
| patch | Perform a one-off |
| post | Perform a one-off |
| put | Perform a one-off |
| trace | Perform a one-off |
Type Definitions
| Exception | A generic error type. |