Crate slinger

source ·
Expand description

GitHubcrates-iodocs-rs

§slinger (投石器)

The slinger crate provides a convenient, low-level HTTP Client.

It handles many of the things that most people just expect an HTTP client to do for them.

Additional learning resources include:

§Making a GET request

For a single request, you can use the get shortcut method.

fn main() -> Result<(), Box<dyn std::error::Error>> {
  let body = slinger::get("https://httpbin.org/get")?
    .text()?;
  println!("body = {body:?}");
  Ok(())
}

NOTE: If you plan to perform multiple requests, it is best to create a Client and reuse it, taking advantage of keep-alive connection pooling.

§Making POST requests (or setting request bodies)

There are several ways you can set the body of a request. The basic one is by using the body() method of a RequestBuilder. This lets you set the exact raw bytes of what the body should be. It accepts various types, including String and Vec<u8>. If you wish to pass a custom type, you can use the slinger::Body constructors.

let client = slinger::Client::new();
let res = client.post("http://httpbin.org/post")
    .body("the exact body that is sent")
    .send()?;

§Redirect Policies

By default, a Client will automatically handle HTTP redirects, having a maximum redirect chain of 10 hops. To customize this behavior, a redirect::Policy can be used with a ClientBuilder.

§Cookies

The automatic storing and sending of session cookies can be enabled with the [cookie_store][client::ClientBuilder::cookie_store] method on ClientBuilder.

§Proxies

 fn run() -> std::result::Result<(), Box<dyn std::error::Error>> {
   let proxy = slinger::Proxy::parse("http://user:pass@127.0.0.1:1080").unwrap();
   // let proxy = slinger::Proxy::parse("socks5://user:pass@127.0.0.1:1080").unwrap();
   let client = slinger::ClientBuilder::new().proxy(proxy).build().unwrap();
   let resp = client.get("https://httpbin.org/get").send()?;
   println!("{:?}", resp);
   Ok(())
 }

§TLS

A Client will use transport layer security (TLS) by default to connect to HTTPS destinations.

  • Additional server certificates can be configured on a ClientBuilder with the [native_tls::Certificate] type.
  • Client certificates can be added to a ClientBuilder with the [native_tls::Identity] type.
  • Various parts of TLS can also be configured or even disabled on the ClientBuilder.

§Optional Features

The following are a list of [Cargo features][cargo-features] that can be enabled or disabled:

  • charset: Improved support for decoding text.
  • cookie: Provides cookie session support.
  • tls: Provides https support.
  • serde: Provides serialization and deserialization support.
  • gzip: Provides response body gzip decompression.

Re-exports§

Modules§

Structs§

Enums§

  • The Errors that may occur when processing a slinger.
  • Configuration of a proxy that a Client should pass requests to.
  • Socket

Functions§

  • Shortcut method to quickly make a GET request.
  • Shortcut method to quickly make a RAW request.

Type Aliases§

  • A Result alias where the Err case is slinger::Error.