Skip to the content.

Deboa Core

The core HTTP client library for Rust, providing a simple yet powerful interface for making HTTP requests.

With Deboa, you can:

Installation

Add to your Cargo.toml:

[dependencies]
deboa = { version = "0.0.9", features = ["http1", "http2", "tokio-rt", "tokio-rust-tls"] }

Features

Basic Usage

use deboa::{Client, request::get, Result};

#[tokio::main]
async fn main() -> Result<(), Result> {
    let client = Client::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(())
}

Making Requests

GET Request

use deboa::request::get;

let response = get("https://api.example.com/data")
    .header("Accept", "application/json")
    .send_with(&client)
    .await?;

// OR

let response = "GET".from_url("https://api.example.com/data")
    .header("Accept", "application/json")
    .send_with(&client)
    .await?;

POST Request with JSON

use deboa_extras::http::serde::json::JsonBody;
use serde_json::json;

let data = json!({ "name": "John Doe", "age": 30 });

let response = deboa::post("https://api.example.com/users")
    .body_as(JsonBody, &data)?
    .send_with(&client)
    .await?;

Handling Responses

#[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(JsonBody)?;

// Get response as text
let text = response.text().await?;

// Get response as bytes
let bytes = response.bytes().await?;

Catchers (Middleware)

Deboa supports middleware for request/response processing:

use deboa::{Result, catcher::DeboaCatcher, request::DeboaRequest, response::DeboaResponse};

struct TestMonitor;

impl DeboaCatcher for TestMonitor {
    async fn on_request(&self, request: &mut DeboaRequest) -> Result<Option<DeboaResponse>> {
        println!("Request: {:?}", request.url());
        Ok(None)
    }

    async fn on_response(&self, response: &mut DeboaResponse) -> Result<()> {
        println!("Response: {:?}", response.status());
        Ok(())
    }
}

// Create a client with middleware
let client = deboa::Client::builder()
    .catch(TestMonitor)
    .build();

Error Handling

Deboa provides comprehensive error handling through the deboa::errors::DeboaError type:

match deboa::get("https://api.example.com/data").send_with(&client).await {
    Ok(response) => {
        // Handle successful response
    }
    Err(DeboaError::Connection(e)) => {
        // Handle connection errors
        eprintln!("Connection failed: {}", e);
    },
    Err(DeboaError::Request(e)) => {
        // Handle request errors
        eprintln!("Request failed: {}", e);
    },
    Err(e) => {
        // Handle other errors
    }
}

Examples

See the examples for more usage examples.

API Reference

For detailed API documentation, see the docs.rs page.