A very simple and straightforward HTTP client written in Rust.
The core HTTP client library for Rust, providing a simple yet powerful interface for making HTTP requests.
Add to your Cargo.toml:
[dependencies]
deboa = { version = "0.0.7", features = ["http1", "tokio-rt"] }
use deboa::{Deboa, request::get};
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = Deboa::new();
// Make a GET request
let response = get("https://httpbin.org/get")
.send_with(&client)
.await?;
println!("Status: {}", response.status());
println!("Body: {}", response.text().await?);
Ok(())
}
let response = deboa::get("https://api.example.com/data")
.header("Accept", "application/json")
.send_with(&client)
.await?;
use serde_json::json;
let data = json!({ "name": "John Doe", "age": 30 });
let response = deboa::post("https://api.example.com/users")
.json(&data)?
.send_with(&client)
.await?;
#[derive(serde::Deserialize)]
struct User {
id: u64,
name: String,
email: String,
}
// Parse JSON response into a struct
let user: User = deboa::get("https://api.example.com/users/1")
.send_with(&client)
.await?
.body_as_json()?;
// Get response as text
let text = response.text().await?;
// Get response as bytes
let bytes = response.bytes().await?;
Deoba supports middleware for request/response processing:
use deboa::middleware::{Middleware, Next};
struct LoggingMiddleware;
#[async_trait::async_trait]
impl Middleware for LoggingMiddleware {
async fn handle(&self, req: deboa::Request, next: Next<'_>) -> Result<deboa::Response, deboa::Error> {
println!("Sending request to {} {}", req.method(), req.uri());
let start = std::time::Instant::now();
let res = next.run(req).await?;
let duration = start.elapsed();
println!("Received response in {:?} - {}", duration, res.status());
Ok(res)
}
}
// Create a client with middleware
let client = deboa::Deboa::builder()
.with(LoggingMiddleware)
.build();
Deoba provides comprehensive error handling through the deboa::Error type:
match deboa::get("https://api.example.com/data").send_with(&client).await {
Ok(response) => {
// Handle successful response
}
Err(deboa::Error::Http(e)) => {
// Handle HTTP errors
}
Err(deboa::Error::Json(e)) => {
// Handle JSON parsing errors
}
Err(e) => {
// Handle other errors
}
}
http1: Enable HTTP/1 support (enabled by default)http2: Enable HTTP/2 supporttokio-rt: Use Tokio as the async runtime (enabled by default)smol-rt: Use smol as the async runtimeSee the examples directory for more usage examples.
For detailed API documentation, see the docs.rs page.