18 releases (5 stable)

2.0.0 Oct 5, 2020
2.0.0-alpha.4 Jun 5, 2020
2.0.0-alpha.0 Mar 2, 2020
1.0.3 Nov 7, 2019
0.0.10 Apr 18, 2019

#2 in Web programming

Download history 5272/week @ 2020-06-30 4601/week @ 2020-07-07 5575/week @ 2020-07-14 4361/week @ 2020-07-21 4445/week @ 2020-07-28 3527/week @ 2020-08-04 5308/week @ 2020-08-11 4374/week @ 2020-08-18 5014/week @ 2020-08-25 4383/week @ 2020-09-01 4895/week @ 2020-09-08 5266/week @ 2020-09-15 5072/week @ 2020-09-22 6533/week @ 2020-09-29 6070/week @ 2020-10-06 5516/week @ 2020-10-13

20,736 downloads per month
Used in 77 crates (73 directly)

MIT/Apache

95KB
1K SLoC

Surf

Surf the web

Built with 🌊 by The http-rs team

Surf is a friendly HTTP client built for casual Rustaceans and veterans alike. 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 Client interface
  • Fully streaming requests and responses
  • TLS/SSL enabled by default
  • Swappable HTTP backends
  • HTTP/2 enabled by default

Examples

#[async_std::main]
async fn main() -> surf::Result<()> {
    let mut res = surf::get("https://httpbin.org/get").await?;
    dbg!(res.body_string().await?);
    Ok(()) 
}

It's also possible to skip the intermediate Response, and access the response type directly.

#[async_std::main]
async fn main() -> surf::Result<()> {
    dbg!(surf::get("https://httpbin.org/get").recv_string().await?);
    Ok(())
}

Both sending and receiving JSON is real easy too.

use serde::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Ip {
    ip: String
}

#[async_std::main]
async fn main() -> surf::Result<()> {
    let uri = "https://httpbin.org/post";
    let data = &Ip { ip: "129.0.0.1".into() };
    let res = surf::post(uri).body(surf::Body::from_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);

    Ok(())
}

And even creating streaming proxies is no trouble at all.

#[async_std::main]
async fn main() -> surf::Result<()> {
    let req = surf::get("https://img.fyi/q6YvNqP").await?;
    let body = surf::http::Body::from_reader(req, None);
    let res = surf::post("https://box.rs/upload").body(body).await?;
    Ok(())
}

Installation

Install OpenSSL -

  • Ubuntu - sudo apt install libssl-dev
  • Fedora - sudo dnf install openssl-devel

Make sure your rust is up to date using: rustup update

With cargo add installed :

$ cargo add surf

Safety

This crate makes use of a single instance of unsafe in order to make the WASM backend work despite the Send bounds. This is safe because WASM targets currently have no access to threads. Once they do we'll be able to drop this implementation, and use a parked thread instead and move to full multi-threading in the process too.

Contributing

Want to join us? Check out our "Contributing" guide and take a look at some of these issues:

See Also

Thanks

Special thanks to prasannavl for donating the crate name, and sagebind for creating an easy to use async curl client that saved us countless hours.

License

MIT OR Apache-2.0

Dependencies

~7–14MB
~297K SLoC