| Crates.io | reqwest-builder |
| lib.rs | reqwest-builder |
| version | 0.2.4 |
| created_at | 2025-07-02 12:04:33.955578+00 |
| updated_at | 2025-07-02 17:55:32.40584+00 |
| description | A builder for reqwest requests with support for custom headers, query parameters, and body content. |
| homepage | |
| repository | https://github.com/FrostPrice/reqwest-builder |
| max_upload_size | |
| id | 1735021 |
| size | 89,778 |
A builder for reqwest requests with support for custom headers, query parameters, and body content, featuring comprehensive error handling.
reqwest-builder-derive crateThe library provides custom error types for error handling:
use reqwest_builder::{IntoReqwestBuilder, ReqwestBuilderError};
// This method returns detailed errors
match request.into_reqwest_builder(&client, &base_url) {
Ok(builder) => {
// Use the builder
}
Err(ReqwestBuilderError::HeaderError { key, value, source }) => {
eprintln!("Invalid header '{}': '{}' - {}", key, value, source);
}
Err(ReqwestBuilderError::SerializationError(msg)) => {
eprintln!("Serialization error: {}", msg);
}
Err(e) => {
eprintln!("Other error: {}", e);
}
}
The library provides detailed error information through the ReqwestBuilderError enum:
SerializationError: Issues with JSON serializationHeaderError: Invalid header names or valuesUrlError: URL construction problemsIoError: File I/O errorsInvalidRequest: General request configuration issuesAdd this to your Cargo.toml:
[dependencies]
reqwest-builder = "0.2.0"
# Optional: For automatic trait implementation
reqwest-builder = { version = "0.2.0", features = ["derive"] }
For easier usage, you can use the reqwest-builder-derive crate to automatically implement the IntoReqwestBuilder trait:
use reqwest_builder::IntoReqwestBuilder;
use serde::Serialize;
#[derive(Serialize, IntoReqwestBuilder)]
#[request(method = "POST", path = "/users/{id}/posts")]
struct CreatePostRequest {
#[path_param]
id: u64,
#[query]
draft: Option<bool>,
#[header(name = "Authorization")]
auth_token: String,
// These fields go into the request body
title: String,
content: String,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build();
let base_url = url::Url::parse("https://api.example.com")?;
let request = CreatePostRequest {
id: 123,
draft: Some(true),
auth_token: "Bearer token123".to_string(),
title: "My Post".to_string(),
content: "Post content here".to_string(),
};
let builder = request.into_reqwest_builder(&client, &base_url)?;
let response = builder.send().await?;
println!("Status: {}", response.status());
Ok(())
}
See the reqwest-builder-derive README for complete documentation on all available attributes and usage patterns.
If you prefer to implement the trait manually or need more control, here's how to do it:
use reqwest_builder::{IntoReqwestBuilder, RequestBody, FileUpload};
use serde::Serialize;
#[derive(Serialize)]
struct CreateUserRequest {
name: String,
email: String,
}
#[derive(Serialize)]
struct AuthHeaders {
#[serde(rename = "Authorization")]
authorization: String,
#[serde(rename = "Content-Type")]
content_type: String,
}
impl IntoReqwestBuilder for CreateUserRequest {
type Headers = AuthHeaders;
fn method(&self) -> http::Method {
http::Method::POST
}
fn endpoint(&self) -> String {
"/users".to_string()
}
fn headers(&self) -> Option<Self::Headers> {
Some(AuthHeaders {
authorization: "Bearer token123".to_string(),
content_type: "application/json".to_string(),
})
}
fn body(&self) -> RequestBody {
RequestBody::Json
}
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let client = reqwest_middleware::ClientBuilder::new(reqwest::Client::new()).build();
let base_url = url::Url::parse("https://api.example.com")?;
let request = CreateUserRequest {
name: "John Doe".to_string(),
email: "john@example.com".to_string(),
};
// Use the manual implementation
let builder = request.into_reqwest_builder(&client, &base_url)?;
let response = builder.send().await?;
println!("Status: {}", response.status());
Ok(())
}
use reqwest_builder::{FileUpload, IntoReqwestBuilder, RequestBody};
// Create file upload with error handling
let file = FileUpload::from_path("document.pdf")?;
// Or create from bytes
let file = FileUpload::from_bytes(
"data.json".to_string(),
b"{}".to_vec(),
Some("application/json".to_string())
);