Crate bitreq

Crate bitreq 

Source
Expand description

§Bitreq

Simple, minimal-dependency HTTP client. The library has a very minimal API, so you’ll probably know everything you need to after reading a few examples.

Note: as a minimal library, bitreq has been written with the assumption that servers are well-behaved. This means that there is little error-correction for incoming data, which may cause some requests to fail unexpectedly. If you’re writing an application or library that connects to servers you can’t test beforehand, consider using a more robust library, such as curl.

§Additional features

Since the crate is supposed to be minimal in terms of dependencies, there are no default features, and optional functionality can be enabled by specifying features for bitreq dependency in Cargo.toml:

[dependencies]
bitreq = { version = "2.13.5-alpha", features = ["punycode"] }

Below is the list of all available features.

§https or https-rustls

This feature uses the (very good) rustls crate to secure the connection when needed. Note that if this feature is not enabled (and it is not by default), requests to urls that start with https:// will fail and return a HttpsFeatureNotEnabled error. https was the name of this feature until the other https feature variants were added, and is now an alias for https-rustls.

§https-rustls-probe

Like https-rustls, but also includes the rustls-native-certs crate to auto-detect root certificates installed in common locations.

§punycode

This feature enables requests to non-ascii domains: the punycode crate is used to convert the non-ascii parts into their punycode representations before making the request. If you try to make a request to 㯙㯜㯙 㯟.net or i❤.ws for example, with this feature disabled (as it is by default), your request will fail with a PunycodeFeatureNotEnabled error.

§async

This feature enables asynchronous HTTP requests using tokio. It provides send_async() and send_lazy_async() methods that return futures for non-blocking operation.

§async-https

Like async, but also enables asynchronous HTTPS support using tokio-rustls. This feature depends on both async and https-rustls features.

§proxy

This feature enables HTTP proxy support. See Proxy.

§urlencoding

This feature enables percent-encoding for the URL resource when creating a request and any subsequently added parameters from Request::with_param.

§Examples

§Get

This is a simple example of sending a GET request and printing out the response’s body, status code, and reason phrase. The ? are needed because the server could return invalid UTF-8 in the body, or something could go wrong during the download.

let response = bitreq::get("http://example.com").send()?;
assert!(response.as_str()?.contains("</html>"));
assert_eq!(200, response.status_code);
assert_eq!("OK", response.reason_phrase);

Note: you could change the get function to head or put or any other HTTP request method: the api is the same for all of them, it just changes what is sent to the server.

§Body (sending)

To include a body, add with_body("<body contents>") before send().

let response = bitreq::post("http://example.com")
    .with_body("Foobar")
    .send()?;

§Headers (sending)

To add a header, add with_header("Key", "Value") before send().

let response = bitreq::get("http://example.com")
    .with_header("Accept", "text/html")
    .send()?;

§Headers (receiving)

Reading the headers sent by the servers is done via the headers field of the Response. Note: the header field names (that is, the keys of the HashMap) are all lowercase: this is because the names are case-insensitive according to the spec, and this unifies the casings for easier get()ing.

let response = bitreq::get("http://example.com").send()?;
assert!(response.headers.get("content-type").unwrap().starts_with("text/html"));

§Timeouts

To avoid timing out, or limit the request’s response time, use with_timeout(n) before send(). The given value is in seconds.

NOTE: There is no timeout by default.

let response = bitreq::post("http://example.com")
    .with_timeout(10)
    .send()?;

§Proxy

To use a proxy server, simply create a Proxy instance and use .with_proxy() on your request.

Supported proxy formats are host:port and user:password@proxy:host. Only HTTP CONNECT proxies are supported at this time.

#[cfg(feature = "proxy")]
{
    let proxy = bitreq::Proxy::new("localhost:8080")?;
    let response = bitreq::post("http://example.com")
        .with_proxy(proxy)
        .send()?;
    println!("{}", response.as_str()?);
}

§Timeouts

By default, a request has no timeout. You can change this in two ways:

  • Use with_timeout on your request to set the timeout per-request like so:
    bitreq::get("/").with_timeout(8).send();
  • Set the environment variable BITREQ_TIMEOUT to the desired amount of seconds until timeout. Ie. if you have a program called foo that uses bitreq, and you want all the requests made by that program to timeout in 8 seconds, you launch the program like so:
    $ BITREQ_TIMEOUT=8 ./foo
    Or add the following somewhere before the requests in the code.
    std::env::set_var("BITREQ_TIMEOUT", "8");

If the timeout is set with with_timeout, the environment variable will be ignored.

Structs§

Proxy
Proxy configuration. Only HTTP CONNECT proxies are supported (no SOCKS or HTTPS).
Request
An HTTP request.
Response
An HTTP response.
ResponseLazy
An HTTP response, which is loaded lazily.

Enums§

Error
Represents an error while sending, receiving, or parsing an HTTP response.
Method
An HTTP request method.

Functions§

connect
Alias for Request::new with method set to Method::Connect.
delete
Alias for Request::new with method set to Method::Delete.
get
Alias for Request::new with method set to Method::Get.
head
Alias for Request::new with method set to Method::Head.
options
Alias for Request::new with method set to Method::Options.
patch
Alias for Request::new with method set to Method::Patch.
post
Alias for Request::new with method set to Method::Post.
put
Alias for Request::new with method set to Method::Put.
trace
Alias for Request::new with method set to Method::Trace.

Type Aliases§

URL
A URL type for requests.