Response
Unified, type-safe API response builder for Axum applications.
This crate provides a simple and consistent way to build JSON API responses in Axum web applications. It ensures a unified format for both success and error responses, making your APIs easier to maintain and consume.
Features
- Type-safe responses: Use generics to ensure your data is serialized correctly.
- Consistent structure: All responses follow the same JSON format with
status, optionaldata, and optionalmessage. - Easy integration: Designed specifically for Axum, with direct support for
IntoResponse. - Lightweight: Minimal dependencies, focused on simplicity.
Installation
Add the following to your Cargo.toml:
[]
= "1"
This crate depends on axum, serde, and serde_json, so ensure they are included in your project as well (or let Cargo handle them via transitive dependencies).
Usage
Import the crate and use the success and error functions in your Axum handlers.
Response Structure
All responses are serialized to JSON in the following format:
Success Response
Use success to return a 200 OK response with data:
use success;
use IntoResponse;
use Serialize;
async
Error Response
Use error to return an error response with a custom status code and message:
use error;
use StatusCode;
use IntoResponse;
async
Examples
A complete demo is available in examples/demo.rs. Here's a snippet:
use StatusCode;
use ;
use ;
use SocketAddr;
use TcpListener;
async
async
async
To run the example:
- Clone the repository:
git clone https://github.com/canmi21/response.git - Navigate to the project:
cd response - Run the example:
cargo run --example demo - Access
http://localhost:8080/successorhttp://localhost:8080/errorin your browser or via curl.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.