A library to create HTTP response content for APIs based on RFC7807.
Get the latest version for your Cargo.toml
from
crates.io.
Add this to your crate root:
rust
extern crate http_api_problem;
## serde
HttpApiProblem
implements Serialize
and Deserialize
for
HttpApiProblem
.
```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 is a conversion between iron
s StatusCode and HttpStatusCode
back
and forth.
The HttpApiProblem
provides a method to_iron_response
which constructs
an iron Response
. If the status
field of the HttpApiProblem
is None
500 - Internal Server Error
is the default.
From<HttpApiProblem
for iron::response::Response
will also be there. It
simply calls to_iron_response
.
Additionally there will be a function into_iron_response
which converts
anything into an iron::response::Response
that can be converted into a
HttpApiProblem
.
There is a conversion between hypers
s StatusCode and HttpStatusCode
back and forth.
The HttpApiProblem
provides a method to_hyper_response
which constructs
a hyper Response
. If the status
field of the HttpApiProblem
is None
500 - Internal Server Error
is the default.
From<HttpApiProblem
for hyper::Response
will also be there. It simply
calls to_hyper_response
.
Additionally there will be a function into_iron_response
which converts
anything into a hyper::Response
that can be converted into a
HttpApiProblem
.
There is a conversion between reqwest
s StatusCode and HttpStatusCode
back and forth.
There is a conversion between rocket
s Status and HttpStatusCode
back
and forth.
HttpApiProblem
implements rocket::response::Responder
, allowing it to
be returned from rocket handlers directly (e.g. as Result<T,
HttpApiProblem>
). It also provides a method to_rocket_response
which
explicitly constructs a rocket Response
. If the status
field of the
HttpApiProblem
is None
500 - Internal Server Error
is the default.
From<HttpApiProblem
for rocket::Response
will also be there. It simply
calls to_rocket_response
.
Additionally there will be a function into_rocket_response
which converts
anything into a rocket::Response
that can be converted into a
HttpApiProblem
.
with_reqwest
addedHttpApiProblem
can now contain additional fieldswith_hyper
returns Responsewith_hyper
returns response Vecwith_hyper
uses hyper 0.12http-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