A library to create HTTP response content for APIs based on RFC7807.
Get the latest version for your Cargo.toml
from
crates.io.
HttpApiProblem
implements Serialize
and Deserialize
.
```rust use httpapiproblem::*; let p = HttpApiProblem::new(StatusCode::UNPROCESSABLEENTITY) .title("You do not have enough credit.") .detail("Your current balance is 30, but that costs 50.") .typeurl("https://example.com/probs/out-of-credit") .instance("/account/12345/msgs/abc");
asserteq!(Some(StatusCode::UNPROCESSABLEENTITY), p.status); asserteq!(Some("You do not have enough credit."), p.title.asderef()); asserteq!(Some("Your current balance is 30, but that costs 50."), p.detail.asderef()); asserteq!(Some("https://example.com/probs/out-of-credit"), p.typeurl.asderef()); asserteq!(Some("/account/12345/msgs/abc"), p.instance.as_deref()); ```
There is also TryFrom<u16>
implemented for [StatusCode]:
```rust use httpapiproblem::*; let p = HttpApiProblem::trynew(422).unwrap() .title("You do not have enough credit.") .detail("Your current balance is 30, but that costs 50.") .typeurl("https://example.com/probs/out-of-credit") .instance("/account/12345/msgs/abc");
asserteq!(Some(StatusCode::UNPROCESSABLEENTITY), p.status); asserteq!(Some("You do not have enough credit."), p.title.asderef()); asserteq!(Some("Your current balance is 30, but that costs 50."), p.detail.asderef()); asserteq!(Some("https://example.com/probs/out-of-credit"), p.typeurl.asderef()); asserteq!(Some("/account/12345/msgs/abc"), p.instance.as_deref()); ```
There are multiple features to integrate with web frameworks:
axum
warp
hyper
actix-web
salvo
tide
rocket
These mainly convert the HttpApiProblem
to response types of
the frameworks and implement traits to integrate with the frameworks
error handling
The feature api-error
enables a structure which can be
return from "api handlers" that generate responses and can be
converted into an HttpApiProblem
.
A big "thank you" for contributions and inspirations goes to the following GitHub users:
http-api-problem
is primarily distributed under the terms of both the MIT
license and the Apache License (Version 2.0).
Copyright (c) 2017 Christian Douven.
License: Apache-2.0/MIT