Expand description
nightfly
The nightfly crate provides a convenient, higher-level HTTP
Client.
It handles many of the things that most people just expect an HTTP client to do for them.
- Reusable, cloneable and serialisable Clients
- Plain bodies, JSON, urlencoded
- Customizable redirect policy
- Uses vm-native [TLS]
- Cookies
Additional learning resources include:
Making a GET request
For a single request, you can use the get shortcut method.
let body = nightfly::get("https://www.rust-lang.org").text();
println!("body = {:?}", body);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 nightfly::Body constructors.
let client = nightfly::Client::new();
let res = client.post("http://httpbin.org/post")
.body("the exact body that is sent")
.send();Forms
It’s very common to want to send form data in a request body. This can be done with any type that can be serialized into form data.
This can be an array of tuples, or a HashMap, or a custom type that
implements Serialize.
// This will POST a body of `foo=bar&baz=quux`
let params = [("foo", "bar"), ("baz", "quux")];
let client = nightfly::Client::new();
let res = client.post("http://httpbin.org/post")
.form(¶ms)
.send();JSON
There is also a json method helper on the RequestBuilder that works in
a similar fashion the form method. It can take any value that can be
serialized into JSON. The feature json is required.
// This will POST a body of `{"lang":"rust","body":"json"}`
let mut map = HashMap::new();
map.insert("lang", "rust");
map.insert("body", "json");
let client = nightfly::Client::new();
let res = client.post("http://httpbin.org/post")
.json(&map)
.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][ClientBuilder::cookie_store] method on ClientBuilder.
Optional Features
The following are a list of Cargo features that can be enabled or disabled:
- cookies: Provides cookie session support.
Modules
- HTTP Cookies
- HTTP header types
- Redirect Handling
Structs
- Body struct
- An http
Clientto make Requests with. - A
ClientBuildercan be used to create aClientwith custom configuration. - The Errors that may occur when processing a
Request. - A set of HTTP headers
- Represents an HTTP header field value.
- Response of an http request
- The Request Method (VERB)
- A request which can be executed with
Client::execute(). - A builder to construct the properties of a
Request. - A Response to a submitted
Request. - An HTTP status code (
status-codein RFC 7230 et al.). - A parsed URL record.
- Represents a version of the HTTP spec.
Traits
- A trait to try to convert some type into a
Url. - Extension trait for http::response::Builder objects
Functions
- Shortcut method to quickly make a
GETrequest.
Type Definitions
- A
Resultalias where theErrcase isnightfly::Error.