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::withtitleandtypefromstatus(HttpStatusCode::NotFound) .setdetail("detailed explanation") .set_instance("/on/1234/do/something");
asserteq!(Some("https://httpstatuses.com/404".tostring()), p.typeurl); asserteq!(Some(HttpStatusCode::NotFound), p.status); asserteq!("Not Found".tostring(), p.title); asserteq!(Some("detailed explanation".tostring()), p.detail); asserteq!(Some("/on/1234/do/something".tostring()), p.instance); ```
There is also From<u16>
implemented for HttpStatusCode
:
```rust use httpapiproblem::*;
let p = HttpApiProblem::withtitleandtypefromstatus(428) .setdetail("detailed explanation") .set_instance("/on/1234/do/something");
asserteq!(Some("https://httpstatuses.com/428".tostring()), p.typeurl); asserteq!(Some(HttpStatusCode::PreconditionRequired), p.status); asserteq!("Precondition Required".tostring(), p.title); asserteq!(Some("detailed explanation".tostring()), p.detail); asserteq!(Some("/on/1234/do/something".tostring()), p.instance); ```
There are multiple features to integrate with web frameworks:
warp
hyper
actix-web
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