DEV Community

Rogério Araújo
Rogério Araújo

Posted on • Edited on

deboa, a simple http client for rust

Few months ago, I started an effort on create a very simple and easy to use http client.

At first, I just tried to mimic the same approach as apisauce for NodeJS, but that didn’t sound appropriate, so I started to refactor to be more rust friendly lib.

Once I moved forward with Deboa implementation, I started to brake it down by features, then by separate crates. Nowadays, Deboa is very modular and can take minimal space on final binary size.

So far, whole Deboa ecosystem is made of following crates:

  • deboa
  • deboa-extras
  • deboa-macros
  • deboa-bora
  • deboa-fory
  • vamo
  • vamo-macros

In this post, we will cover Deboa and some of its features.

Features

Right now Deboa has the following features:

  • tokio-rt
  • smol-rt
  • http1
  • http2

All the examples on this post are based on tokio-rt and http1.

Adding Deboa to your project

You can easy add Deboa to your rust project using cargo, like so:

cargo add Deboa
Enter fullscreen mode Exit fullscreen mode

Initializing client

Create a new client instance can be simple as call Deboa::new(), but you can also setup interceptors, protocol to use, among other things.

Example

use deboa::{Deboa, Result};

#[tokio::main]
async fn main() -> Result<()> {
    let mut client = Deboa::new();
}
Enter fullscreen mode Exit fullscreen mode

Making requests

You have many ways to create a request within Deboa, the most common way is by initializing DeboaResponse.

There are several ways to create a instance of DeboaRequest:

  • by using get, post, delete, put and patch request module functions
  • by using FetchWith trait
  • by taking advantage of IntoRequest trait

Example

use deboa::{
    Deboa, Result, request::{DeboaRequest, FetchWith, get}
};

#[tokio::main]
async fn main() -> Result<()> {
    let mut client = Deboa::new();    
    let req = get("https://jsonplaceholder.typicode.com/posts")?;

    // process request
}
Enter fullscreen mode Exit fullscreen mode

Reading responses

Once request is issued with send_with, fetch_with or Deboa::execute, you will have access to DeboaResponse. Within DeboaResponse you can check status_code, cookies, headers and read response body.

Example

use deboa::{
    Deboa, Result, request::{DeboaRequest, FetchWith, get}
};

#[tokio::main]
async fn main() -> Result<()> {
    let mut client = Deboa::new();    
    let req = get("https://jsonplaceholder.typicode.com/posts")?;
    let res = req
      .send_with(client)
      .await?
      .raw_body()
      .await?;

    // do something with bytes
}
Enter fullscreen mode Exit fullscreen mode

Final thoughts

As we can see, with Deboa you can easily initialize client, customize it, create request in several different ways and read responses in a very feel steps.

I strongly recommend you to visit deboa repository, try examples, and leave your star if you like it!

Github: https://github.com/ararog/deboa

Top comments (4)

Collapse
 
thiagomg profile image
Thiago Massari Guedes

Nice! Adorei o nome

Collapse
 
rogrio_arajo_55dae16f0d profile image
Rogério Araújo

😁

Collapse
 
luiz_mitidiero_007499f0a6 profile image
Luiz Mitidiero

Great! Cool name and pretty useful!

Collapse
 
rogrio_arajo_55dae16f0d profile image
Rogério Araújo

Thanks! I really appreciate!