Reqwest Builder Derive
A derive macro for the reqwest-builder crate that automatically generates IntoReqwestBuilder implementations for your structs.
Overview
This crate provides the IntoReqwestBuilder derive macro that automatically implements the trait for your request structs, eliminating the need for manual implementation. Simply annotate your struct with attributes to define HTTP request properties.
Features
- Automatic Implementation: Derive
IntoReqwestBuildertrait automatically - Flexible Attributes: Control every aspect of your HTTP request through attributes
- Path Parameters: Replace placeholders in URLs with struct fields
- Query Parameters: Automatically handle query string generation
- Custom Headers: Define headers with custom names
- Multiple Body Types: Support for JSON, form, multipart, and no-body requests
- Type Safety: Compile-time validation of request structure
Installation
Add this to your Cargo.toml:
[]
= { = "0.2.0", = ["derive"] }
= { = "1.0", = ["derive"] }
Usage
Basic Example
use ;
use IntoReqwestBuilder;
use Serialize;
async
Attributes Reference
Container Attributes
These attributes are applied to the struct itself:
#[request(method = "...")] (Required)
Specifies the HTTP method for the request.
Supported methods:
GETPOSTPUTDELETEPATCHHEADOPTIONS
#[request(path = "...")] (Required)
Specifies the endpoint path. Can include placeholders for path parameters.
#[request(body = "...")] (Optional)
Specifies how the request body should be encoded. Defaults to "json".
Supported body types:
"json"- JSON encoding (default)"form"- Form URL encoding"multipart"- Multipart form data"none"- No request body
Field Attributes
These attributes are applied to individual struct fields:
#[path_param]
Marks a field as a path parameter. The field's value will replace {field_name} in the path.
#[query] and #[query(name = "...")]
Marks a field as a query parameter.
This generates a URL like: /posts?page=1&per_page=10&published=true
#[header] and #[header(name = "...")]
Marks a field as a request header.
#[body]
Explicitly marks a field to be included in the request body. This is the default behavior for unmarked fields, so it's usually not necessary.
Advanced Examples
Complex Request with All Features
Form-Encoded Request
GET Request with Query Parameters Only
Type Compatibility
The derive macro works with various Rust types:
- Primitive types:
String,u32,i64,bool, etc. - Option types:
Option<T>for optional query parameters and headers - Collections:
Vec<T>,HashMap<K, V>(in body) - Custom types: Any type that implements
Serialize(for body fields)
Error Handling
The generated implementation works with both error handling approaches provided by reqwest-builder:
// Explicit error handling (recommended)
match request.try_into_reqwest_builder
// Backward compatible (silent failures)
let builder = request.into_reqwest_builder;
Generated Code
The derive macro generates:
- A companion
Headersstruct for typed header management - Implementation of all
IntoReqwestBuildertrait methods:method()- Returns the HTTP methodendpoint()- Builds the URL with path parameter substitutionheaders()- Creates headers from annotated fieldsquery_params()- Builds query parameters from annotated fieldsbody()- Specifies the body encoding type
Requirements
- Rust 2024 edition or later
serdewithderivefeature for serializationreqwest-builderas the main crate
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.