fetchttp-0.1.0 has been yanked.
fetchttp
A WHATWG Fetch API compliant HTTP client library for Rust that provides the familiar fetch()
interface you know and love from web development.
⨠Features
- đ WHATWG Fetch API Compliant - Follows the official specification
- đ Async/Await Support - Built on Tokio for modern async Rust
- đ Type Safety - Leverages Rust's type system for safe HTTP operations
- đĻ JSON Support - Built-in JSON serialization/deserialization with serde
- đ§ Flexible Bodies - Support for text, bytes, and JSON request/response bodies
- đ Header Management - Complete header manipulation API
- đ Request/Response Cloning - Efficient cloning following the specification
- âšī¸ Abort Signals - Request cancellation support
- đ Connection Pooling - Automatic connection reuse for better performance
đ Quick Start
Add fetchttp
to your Cargo.toml
:
[]
= "0.1.0"
= { = "1.0", = ["full"] }
= "1.0" # For JSON support
Simple GET Request
use *;
async
POST Request with JSON
use *;
use json;
async
Custom Headers
use *;
async
Request Cancellation
use *;
use Duration;
use sleep;
async
đ API Reference
Core Functions
fetch(url, init)
- Perform an HTTP request
Request Types
Request
- Represents an HTTP requestRequestInit
- Configuration for requestsRequestMode
- CORS mode settingsRequestCredentials
- Credential handlingRequestCache
- Cache controlRequestRedirect
- Redirect handling
Response Types
Response
- Represents an HTTP responseResponseInit
- Configuration for responsesResponseType
- Response type information
Body and Streams
ReadableStream
- Request/response body handlingHeaders
- HTTP header management
Error Handling
FetchError
- Main error typeTypeError
- Type validation errorsNetworkError
- Network-related errorsAbortError
- Request cancellation errors
Abort Support
AbortController
- Controls request cancellationAbortSignal
- Signals request cancellation
đ Body Consumption
Response and request bodies can be consumed in multiple ways:
// Text
let text = response.text.await?;
// JSON
let data: MyStruct = response.json.await?;
// Bytes
let bytes = response.array_buffer.await?;
// Blob (alias for array_buffer)
let blob = response.blob.await?;
đĄī¸ Error Handling
The library provides comprehensive error handling:
match fetch.await
đ§ Advanced Usage
Custom HTTP Methods
let mut init = new;
init.method = Some;
init.body = Some;
let response = fetch.await?;
File Upload
use fs;
let file_content = read?;
let mut init = new;
init.method = Some;
init.body = Some;
let response = fetch.await?;
Response Headers
let response = fetch.await?;
for in response.headers.entries
// Check specific header
if let Some = response.headers.get?
đī¸ Building and Testing
# Build the library
# Run tests
# Run benchmarks
# Generate documentation
đ Performance
The library is designed for performance with:
- Connection pooling via hyper
- Efficient body streaming
- Zero-copy operations where possible
- Minimal allocations
See benches/
for detailed performance benchmarks.
đ¤ Contributing
Contributions are welcome! Please see CONTRIBUTING.md for guidelines.
đ License
This project is licensed under the MIT License - see the LICENSE file for details.
đ Comparison with Other Libraries
Feature | fetchttp | reqwest | hyper | ureq |
---|---|---|---|---|
WHATWG Fetch API | â | â | â | â |
Async/Await | â | â | â | â |
JSON Support | â | â | â | â |
Connection Pooling | â | â | â | â |
Abort Signals | â | â | â | â |
Web API Compatibility | â | â | â | â |
đ Links
Made with â¤ī¸ by MuntasirSZN