barehttp
A minimal, explicit HTTP client for Rust
barehttp is a low-level, blocking HTTP client designed for developers who want predictable behavior, full control, and minimal dependencies.
It supports no_std (with alloc), avoids global state, and exposes all network
behavior explicitly. There is no async runtime, no built-in TLS—you bring your own
via adapters.
Key Features
- Minimal and explicit: No global state, no implicit behavior
no_stdcompatible: Core works withalloconly- Blocking I/O: Simple, predictable execution model
- Generic adapters: Custom socket and DNS implementations
- Compile-time safety: Typestate enforces correct body usage
Quick Start
use ResponseExt;
let response = get?;
let body = response.text?;
println!;
Using HttpClient
For repeated requests or more control, use HttpClient:
use HttpClient;
use ResponseExt;
let mut client = new?;
let response = client
.get
.header
.call?;
println!;
println!;
Configuration
Client behavior is controlled via Config and ConfigBuilder:
use HttpClient;
use ConfigBuilder;
use Duration;
let config = new
.timeout
.max_redirects
.user_agent
.build;
let mut client = with_config?;
let response = client.get.call?;
Making Requests
GET
let mut client = new?;
let response = client.get.call?;
Connection Pooling
Connection pooling is enabled by default for better performance:
use ConfigBuilder;
use Duration;
// Customize pooling behavior
let config = new
.connection_pooling // enabled by default
.max_idle_per_host // max idle connections per host
.idle_timeout // idle timeout
.build;
let mut client = with_config?;
// First request creates a new connection
let resp1 = client.get.call?;
// Second request reuses the pooled connection
let resp2 = client.get.call?;
// Disable pooling for one-off requests
let config = new
.connection_pooling
.build;
POST with body
let mut client = new?;
let response = client
.post
.header
.send?;
Type-Safe Request Bodies
Request methods enforce body semantics at compile time:
- GET, HEAD, DELETE, OPTIONS → methods without body
- POST, PUT, PATCH → methods with body
// This won't compile:
let mut client = new?;
client.get.send?;
// This compiles - POST requires .send():
let mut client = new?;
client.post.send?;
Error Handling
All operations return Result<T, barehttp::Error>.
Errors include:
- DNS resolution failures
- Socket I/O errors
- Parse errors
- HTTP status errors (4xx/5xx by default)
use ;
let mut client = new?;
match client.get.call
Automatic HTTP status errors can be disabled via configuration.
Response Helpers
The ResponseExt trait provides helpers for common tasks:
use ResponseExt;
let mut client = new?;
let response = client.get.call?;
if response.is_success
Custom Socket and DNS Adapters
barehttp's networking is fully pluggable.
Implement BlockingSocket and DnsResolver traits to provide:
- TLS via external libraries
- Proxies or tunnels
- Embedded or WASM networking
- Test mocks
use ;
let dns = new;
let mut client: = new_with_adapters;
let response = client.get.call?;
Implementing Custom Sockets
For embedded systems or custom networking, implement BlockingSocket:
use BlockingSocket;
use SocketError;
// Use with HttpClient
let dns = new;
let client: = new_with_adapters;
The pool will call MyCustomSocket::new() internally when it needs to create connections.
Design Notes
- Blocking I/O keeps the API simple and dependency-free
- Connection pooling reuses TCP connections for better performance (configurable)
- Generic adapters allow custom socket implementations via trait
- Explicit behavior with no hidden global state
barehttp is intended for environments where clarity and control matter more than convenience.
License
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.
Contribution
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in the work by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.