Giter Site home page Giter Site logo

surf's Introduction

Surf

Surf the web

Built with ๐ŸŒŠ by The http-rs team

surf the web.

Surf is a Rust HTTP client built for ease-of-use and multi-HTTP-backend flexibility. Whether it's a quick script, or a cross-platform SDK, Surf will make it work.

  • Extensible through a powerful middleware system
  • Multiple HTTP back-ends that can be chosen
  • Reuses connections through a configurable Client interface
  • Fully streaming requests and responses
  • TLS enabled by default (native tls or rustls)
  • Built on async-std (with optional tokio support)

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 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?;

Setting configuration on a client is also straightforward.

use std::convert::TryInto;
use std::time::Duration;
use surf::{Client, Config};
use surf::Url;

let client: Client = Config::new()
    .set_base_url(Url::parse("http://example.org")?)
    .set_timeout(Some(Duration::from_secs(5)))
    .try_into()?;

let mut res = client.get("/").await?;
println!("{}", res.body_string().await?);

Features

The following features are available. The default features are curl-client, middleware-logger, and encoding

  • curl-client (default): use curl (through isahc) as the HTTP backend.
  • h1-client: use async-h1 as the HTTP backend with native TLS for HTTPS.
  • h1-client-rustls: use async-h1 as the HTTP backend with rustls for HTTPS.
  • hyper-client: use hyper (hyper.rs) as the HTTP backend.
  • wasm-client: use window.fetch as the HTTP backend.
  • middleware-logger (default): enables logging requests and responses using a middleware.
  • encoding (default): enables support for body encodings other than utf-8.

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

surf's People

Contributors

fishrock123 avatar yoshuawuyts avatar goto-bus-stop avatar jbr avatar k-nasa avatar fairingrey avatar taiki-e avatar mustafapc19 avatar dignifiedquire avatar tirr-c avatar rossdylan avatar jkelleyrtp avatar azanellato avatar utsavm9 avatar hipstermojo avatar zkat avatar joshtriplett avatar licenser avatar cjpearce avatar brightly-salty avatar tmcw avatar tako8ki avatar shuhei avatar icewind1991 avatar rtyler avatar philiptrauner avatar zyctree avatar killercup avatar oli-cosmian avatar nhellwig avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.