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
- 🎯 Type-safe error handling
Installation
Add this to your Cargo.toml:
[]
= "0.2.1"
Quick Start
use Request;
use Duration;
async
Advanced Usage
Setting Default Headers
use ;
use ;
let mut headers = new;
headers.insert;
headers.insert;
let client = new?
.set_default_headers;
POST Request with JSON
use json;
let response = client.post
.json
.send
.await?;
Streaming Response
use StreamExt;
let mut stream = client.get
.send
.await?
.bytes_stream;
while let Some = stream.next.await
Error Handling
use RequestError;
match client.get.send.await
API Reference
Request Methods
new(base_url: &str)- Create a new Request clientget(path: &str)- Create a GET requestpost(path: &str)- Create a POST requestput(path: &str)- Create a PUT requestpatch(path: &str)- Create a PATCH requestdelete(path: &str)- Create a DELETE request
Configuration Methods
set_timeout(duration: Duration)- Set request timeoutset_default_headers(headers: HeaderMap)- Set default headers for all requests
Request Builder Methods
header(key: K, value: V)- Add a header to the requestheaders(headers: HeaderMap)- Add multiple headersquery<T: Serialize>(query: &T)- Add query parametersjson<T: Serialize>(json: &T)- Set JSON bodybody<T: Into<Body>>(body: T)- Set request bodysend()- Execute the request
Response Methods
status()- Get response status codeheaders()- Get response headerstext()- Get response body as textjson<T: DeserializeOwned>()- Parse response as JSONbytes()- Get response body as bytesbytes_stream()- Get response as a byte stream
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.