8 releases

new 1.0.0-beta.3 Dec 9, 2024
1.0.0-beta.2 Nov 21, 2024
1.0.0-beta.1 Oct 3, 2024
1.0.0-alpha.16+croatian-pine-marten Feb 26, 2024

#155 in HTTP client

Download history 27/week @ 2024-09-22 148/week @ 2024-09-29 20/week @ 2024-10-06 15/week @ 2024-10-13 3/week @ 2024-10-20 3/week @ 2024-10-27 8/week @ 2024-11-03 134/week @ 2024-11-17 24/week @ 2024-11-24 3/week @ 2024-12-01 141/week @ 2024-12-08

302 downloads per month
Used in flawless-slack

BSD-2-Clause-Patent

30KB
397 lines

flawless-http

docs.rs docs crates.io version

HTTP client for https://flawless.dev.

This library takes a lot of inspiration from ureq.

Quick start

A simple GET request would look like this.

use flawless::workflow;
use flawless_http::get;

#[workflow("fetch")]
fn fetch() {
    let request = get("https://example.com").set_header("Accept", "application/json");
    let response = request.send().unwrap();
    log::info!("{}", String::from_utf8(response.body()).unwrap());
}

Body

A request can contain a body, but depending on the body type, additional request headers might be set. If headers are already manually set by the user, they are not overwritten.

Text arguments

In case a String or &str type is given to body, the "Content-Length" header is going to be set to the byte length of the string.

use flawless_http::post;

let response = post("https://httpbin.org/post")
                .body("Hello world!")
                .send();
assert!(response.is_ok());
Form arguments

In case a slice of tuples of strings is passed in (&[(&str, &str)]), body will assume a for is being submitted and URL encode it. It will set the header "Content-Type" to "application/x-www-form-urlencoded", and "Content-Length" to the size of the encoded content.

use flawless_http::post;

let response = post("https://httpbin.org/post")
                .body([
                  ("Hello", "world!"),
                  ("second", "argument"),
                ].as_ref())
                .send();
assert!(response.is_ok());
JSON arguments

In case of a serde_json::Value type, body will assume that the content is of type JSON and set the header "Content-Type" to "application/json". It will also set "Content-Length" to the size of the serialized JSON.

use flawless_http::post;
use serde_json::json;

let response = post("https://httpbin.org/post")
                .body(json!({
                    "Hello": "world!",
                    "second": "argument",
                }))
                .send();
assert!(response.is_ok());

Dependencies

~1–2MB
~41K SLoC