Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/nis5l/rocketjson
Crate for working with Json and Rocket.
https://github.com/nis5l/rocketjson
api json rocket rust server web
Last synced: 28 days ago
JSON representation
Crate for working with Json and Rocket.
- Host: GitHub
- URL: https://github.com/nis5l/rocketjson
- Owner: Nis5l
- Created: 2021-08-29T10:52:59.000Z (about 3 years ago)
- Default Branch: master
- Last Pushed: 2022-06-27T12:26:50.000Z (over 2 years ago)
- Last Synced: 2024-09-28T15:41:52.995Z (about 1 month ago)
- Topics: api, json, rocket, rust, server, web
- Language: Rust
- Homepage:
- Size: 91.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# rocketjson
[![Current Crates.io Version](https://img.shields.io/crates/v/rocketjson.svg)](https://crates.io/crates/rocketjson)Crate for working with Json and [Rocket](https://github.com/SergioBenitez/Rocket). \
Ultimately the goal is to have [validated](https://github.com/Keats/validator) Structs enter and leave the endpoint as Json
while having everything happen in the background.# Documentation
Documentation is on [docs.rs](https://docs.rs/rocketjson)# Example
```
#[macro_use] extern crate rocket;#[derive(serde::Deserialize, validator::Validate, rocketjson::JsonBody)]
pub struct RegisterRequest {
#[validate(length(min = 1))]
username: String
}#[derive(serde::Serialize)]
pub struct RegisterResponse {
message: String
}#[post("/register", data="")]
pub fn register(data: RegisterRequest) -> rocketjson::ApiResponse {
rocketjson::ApiResponse::new(rocket::http::Status::Ok, RegisterResponse { message: format!("Welcome {}", data.username) })
}#[launch]
fn rocket() -> _ {
rocket::build()
.mount("/", routes![register]).
register("/", vec![rocketjson::error::get_catcher()])
}
```
- Input
```
{
"username": "testuser"
}
```
- Output 200 OK
```
{
"message": "Welcome testuser"
}
```
- Input
```
{
"username": ""
}
```
- Output 400 Bad Request
```
{
"username": [
{
"code": "length",
"message": null,
"params": {
"value": "",
"min": 1
}
}
]
}
```
# License
The license can be chosen to be either of the following:
- [MIT](https://opensource.org/licenses/MIT)
- [Apache-2.0](https://www.apache.org/licenses/LICENSE-2.0)