| Crates.io | toolcraft-request |
| lib.rs | toolcraft-request |
| version | 0.2.2 |
| created_at | 2025-08-05 03:57:20.337449+00 |
| updated_at | 2025-08-05 04:03:46.312405+00 |
| description | Toolcraft request module |
| homepage | |
| repository | https://github.com/code-serenade/toolcraft.git |
| max_upload_size | |
| id | 1781510 |
| size | 57,902 |
A lightweight, ergonomic HTTP client wrapper around reqwest with support for base URLs, default headers, and streaming responses.
Add this to your Cargo.toml:
[dependencies]
toolcraft-request = "0.2.1"
use toolcraft_request::Request;
use std::time::Duration;
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
// Create a new request client
let client = Request::new("https://api.example.com")?
.set_timeout(Duration::from_secs(30));
// Make a simple GET request
let response = client.get("/users")
.header("User-Agent", "MyApp/1.0")
.send()
.await?;
println!("Status: {}", response.status());
println!("Body: {}", response.text().await?);
Ok(())
}
use toolcraft_request::{Request, HeaderMap};
use reqwest::header::{AUTHORIZATION, USER_AGENT};
let mut headers = HeaderMap::new();
headers.insert(AUTHORIZATION, "Bearer token123".parse()?);
headers.insert(USER_AGENT, "MyApp/1.0".parse()?);
let client = Request::new("https://api.example.com")?
.set_default_headers(headers);
use serde_json::json;
let response = client.post("/users")
.json(&json!({
"name": "John Doe",
"email": "john@example.com"
}))
.send()
.await?;
use futures_util::StreamExt;
let mut stream = client.get("/large-file")
.send()
.await?
.bytes_stream();
while let Some(chunk) = stream.next().await {
let chunk = chunk?;
println!("Received {} bytes", chunk.len());
}
use toolcraft_request::RequestError;
match client.get("/api/data").send().await {
Ok(response) => println!("Success: {}", response.status()),
Err(RequestError::Network(e)) => eprintln!("Network error: {}", e),
Err(RequestError::Timeout) => eprintln!("Request timed out"),
Err(e) => eprintln!("Other error: {}", e),
}
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 requestset_timeout(duration: Duration) - Set request timeoutset_default_headers(headers: HeaderMap) - Set default headers for all requestsheader(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 requeststatus() - 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 streamThis project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.