attohttpc/
lib.rs

1#![deny(missing_debug_implementations)]
2#![deny(missing_docs)]
3#![allow(clippy::needless_doctest_main)]
4//! This project's goal is to provide a lightweight and simple HTTP client for the Rust ecosystem. The intended use is for
5//! projects that have HTTP needs where performance is not critical or when HTTP is not the main purpose of the application.
6//! Note that the project still tries to perform well and avoid allocation where possible, but stays away from Rust's
7//! asynchronous stack to provide a crate that's as small as possible. Features are provided behind feature flags when
8//! possible to allow users to get just what they need.
9//!
10//! Check out the [repository](https://github.com/sbstp/attohttpc) for more information and examples.
11//!
12//! # Quick start
13//! ```no_run
14//! # #[cfg(feature = "json")]
15//! # use serde_json::json;
16//! # #[cfg(feature = "json")]
17//! # fn main() -> attohttpc::Result {
18//! let obj = json!({
19//!     "hello": "world",
20//! });
21//!
22//! let resp = attohttpc::post("https://my-api.org/do/something")
23//!     .header("X-My-Header", "foo")   // set a header for the request
24//!     .param("qux", "baz")            // set a query parameter
25//!     .json(&obj)?                    // set the request body (json feature required)
26//!     .send()?;                       // send the request
27//!
28//! // Check if the status is a 2XX code.
29//! if resp.is_success() {
30//!     // Consume the response body as text and print it.
31//!     println!("{}", resp.text()?);
32//! }
33//! # Ok(())
34//! # }
35//! # #[cfg(not(feature = "json"))]
36//! # fn main() {
37//! # }
38//! ```
39//!
40//! # Features
41//! * `basic-auth` support for basic auth
42//! * `charsets` support for decoding more text encodings than just UTF-8
43//! * `compress` support for decompressing response bodies using `miniz_oxide` (**default**)
44//! * `compress-zlib` support for decompressing response bodies using `zlib` instead of `miniz_oxide`
45//!   (see [flate2 backends](https://github.com/rust-lang/flate2-rs#backends))
46//! * `compress-zlib-ng` support for decompressing response bodies using `zlib-ng` instead of `miniz_oxide`
47//!   (see [flate2 backends](https://github.com/rust-lang/flate2-rs#backends))
48//! * `json` support for serialization and deserialization
49//! * `form` support for url encoded forms (does not include support for multipart)
50//! * `multipart-form` support for multipart forms (does not include support for url encoding)
51//! * `tls-native` support for tls connections using the `native-tls` crate (**default**)
52//! * `tls-native-vendored` activate the `vendored` feature of `native-tls`
53//! * `tls-rustls-webpki-roots` support for TLS connections using `rustls` instead of `native-tls` with Web PKI roots
54//! * `tls-rustls-native-roots` support for TLS connections using `rustls` with root certificates loaded from the `rustls-native-certs` crate
55//!
56//! # Activating a feature
57//! To activate a feature, specify it in your `Cargo.toml` file like so
58//! ```toml
59//! attohttpc = { version = "...", features = ["json", "form", ...] }
60//! ```
61//!
62
63macro_rules! debug {
64    ($($arg:tt)+) => { log::debug!(target: "attohttpc", $($arg)+) };
65}
66
67macro_rules! warn {
68    ($($arg:tt)+) => { log::warn!(target: "attohttpc", $($arg)+) };
69}
70
71#[cfg(feature = "charsets")]
72pub mod charsets;
73mod error;
74mod happy;
75#[cfg(feature = "multipart-form")]
76mod multipart;
77#[cfg(feature = "multipart-form")]
78mod multipart_crate;
79mod parsing;
80mod request;
81mod streams;
82mod tls;
83
84pub use crate::error::{Error, ErrorKind, InvalidResponseKind, Result};
85#[cfg(feature = "multipart-form")]
86pub use crate::multipart::{Multipart, MultipartBuilder, MultipartFile};
87pub use crate::parsing::{Response, ResponseReader};
88pub use crate::request::proxy::{ProxySettings, ProxySettingsBuilder};
89pub use crate::request::{body, PreparedRequest, RequestBuilder, RequestInspector, Session};
90#[cfg(feature = "charsets")]
91pub use crate::{charsets::Charset, parsing::TextReader};
92pub use http::Method;
93pub use http::StatusCode;
94
95pub mod header {
96    //! This module is a re-export of the `http` crate's `header` module.
97    pub use http::header::*;
98}
99
100/// Create a new `RequestBuilder` with the GET method.
101pub fn get<U>(base_url: U) -> RequestBuilder
102where
103    U: AsRef<str>,
104{
105    RequestBuilder::new(Method::GET, base_url)
106}
107
108/// Create a new `RequestBuilder` with the POST method.
109pub fn post<U>(base_url: U) -> RequestBuilder
110where
111    U: AsRef<str>,
112{
113    RequestBuilder::new(Method::POST, base_url)
114}
115
116/// Create a new `RequestBuilder` with the PUT method.
117pub fn put<U>(base_url: U) -> RequestBuilder
118where
119    U: AsRef<str>,
120{
121    RequestBuilder::new(Method::PUT, base_url)
122}
123
124/// Create a new `RequestBuilder` with the DELETE method.
125pub fn delete<U>(base_url: U) -> RequestBuilder
126where
127    U: AsRef<str>,
128{
129    RequestBuilder::new(Method::DELETE, base_url)
130}
131
132/// Create a new `RequestBuilder` with the HEAD method.
133pub fn head<U>(base_url: U) -> RequestBuilder
134where
135    U: AsRef<str>,
136{
137    RequestBuilder::new(Method::HEAD, base_url)
138}
139
140/// Create a new `RequestBuilder` with the OPTIONS method.
141pub fn options<U>(base_url: U) -> RequestBuilder
142where
143    U: AsRef<str>,
144{
145    RequestBuilder::new(Method::OPTIONS, base_url)
146}
147
148/// Create a new `RequestBuilder` with the PATCH method.
149pub fn patch<U>(base_url: U) -> RequestBuilder
150where
151    U: AsRef<str>,
152{
153    RequestBuilder::new(Method::PATCH, base_url)
154}
155
156/// Create a new `RequestBuilder` with the TRACE method.
157pub fn trace<U>(base_url: U) -> RequestBuilder
158where
159    U: AsRef<str>,
160{
161    RequestBuilder::new(Method::TRACE, base_url)
162}
163
164mod skip_debug {
165    use std::fmt;
166
167    #[derive(Clone)]
168    pub struct SkipDebug<T>(pub T);
169
170    impl<T> fmt::Debug for SkipDebug<T> {
171        fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
172            write!(f, "...")
173        }
174    }
175
176    impl<T> From<T> for SkipDebug<T> {
177        fn from(val: T) -> SkipDebug<T> {
178            SkipDebug(val)
179        }
180    }
181}