toolcraft-request
A lightweight, ergonomic HTTP client wrapper around reqwest with support for base URLs, default headers, and streaming responses.
Features
- 🚀 Simple and intuitive API
- 🔧 Base URL configuration for API clients
- 📋 Default headers management
- ⏱️ Configurable timeouts
- 🌊 Stream response support
- 📤 Multipart/form-data file upload
- 🎯 Type-safe error handling
Installation
Add this to your Cargo.toml:
[]
= "*"
Check the crates.io page for the latest version.
Quick Start
use Request;
use json;
async
Advanced Usage
Setting Default Headers
use ;
let mut client = new?;
client.set_base_url?;
// Method 1: Build headers manually
let mut headers = new;
headers.insert?;
headers.insert?;
client.set_default_headers;
// Method 2: Use preset for JSON APIs
let headers = for_json?;
client.set_default_headers;
// Sets: Content-Type: application/json
// Accept: application/json
// Method 3: Use preset for form uploads
let headers = for_form;
client.set_default_headers;
// Returns empty HeaderMap (Content-Type handled by post_form)
GET Request with Query Parameters
let query = vec!;
let response = client.get.await?;
POST Request with JSON
use json;
let body = json!;
let response = client.post.await?;
POST with Custom Headers (Dynamic Values)
use HeaderMap;
use json;
// Support dynamic header values
let token = get_auth_token.await?;
let mut headers = new;
headers.insert?;
headers.insert?;
let body = json!;
let response = client.post.await?;
File Upload with FormData
use ;
let mut client = new?;
client.set_base_url?;
// Method 1: Upload file from path
let fields = vec!;
let response = client.post_form.await?;
// Method 2: Upload from bytes
let file_data = read?;
let fields = vec!;
let response = client.post_form.await?;
Important: post_form() automatically removes Content-Type header to let reqwest set the correct multipart/form-data with boundary.
Managing Headers
use HeaderMap;
// Create with presets
let mut headers = for_json?; // JSON API preset
// Or: let headers = HeaderMap::for_form(); // Form upload preset
// Or: let mut headers = HeaderMap::new(); // Empty
// Insert headers (supports dynamic strings)
headers.insert?;
let custom_header = "X-Request-ID".to_string;
headers.insert?;
// Check if header exists
if headers.contains
// Get header value
if let Some = headers.get
// Remove header
let removed = headers.remove;
assert_eq!;
// Merge headers
let mut other_headers = new;
other_headers.insert?;
headers.merge;
Streaming Response
use StreamExt;
use json;
let body = json!;
let mut stream = client.post_stream.await?;
while let Some = stream.next.await
Error Handling
match client.get.await
API Reference
Creating a Client
Request::new()- Create a new Request clientRequest::with_timeout(timeout_sec: u64)- Create client with timeout
Configuration Methods
set_base_url(&mut self, base_url: &str)- Set base URL for all requestsset_default_headers(&mut self, headers: HeaderMap)- Set default headers
HTTP Methods
-
get(endpoint, query, headers)- Send GET requestendpoint: &str- API endpointquery: Option<Vec<(String, String)>>- Query parametersheaders: Option<HeaderMap>- Custom headers
-
post(endpoint, body, headers)- Send POST request with JSONendpoint: &str- API endpointbody: &serde_json::Value- JSON bodyheaders: Option<HeaderMap>- Custom headers
-
put(endpoint, body, headers)- Send PUT request with JSON -
delete(endpoint, headers)- Send DELETE request -
post_form(endpoint, form_fields, headers)- Send POST with multipart/form-dataendpoint: &str- API endpointform_fields: Vec<FormField>- Form fields (text and files)headers: Option<HeaderMap>- Custom headers
-
post_stream(endpoint, body, headers)- Send POST and return byte stream
FormField Methods
FormField::text(name, value)- Create text fieldFormField::file(name, path)- Create file field from path (async)FormField::file_from_bytes(name, filename, content)- Create file field from bytes
HeaderMap Methods
Factory Methods:
new()- Create new empty HeaderMapfor_json()- Create with JSON preset headers (Content-Type + Accept)for_form()- Create for form uploads (empty, Content-Type auto-handled)
Management Methods:
insert(key, value)- Insert header (supports dynamic strings, overwrites if exists)get(key)- Get header value as Stringremove(key)- Remove header and return its valuecontains(key)- Check if header existsmerge(other)- Merge another HeaderMap (overwrites on conflict)
Response Methods
status()- Get HTTP status codeheaders()- Get response headerstext()- Get response as text (async)json<T>()- Parse response as JSON (async)bytes()- Get response as bytes (async)
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.