Crate http_api_problem[][src]

HTTP-API-PROBLEM

crates.io docs.rs downloads CI license-mit license-apache

A library to create HTTP response content for APIs based on RFC7807.

Usage

Get the latest version for your Cargo.toml from crates.io.

Add this to your crate root:

extern crate http_api_problem;

serde

HttpApiProblem implements Serialize and Deserialize for HttpApiProblem.

Examples

use http_api_problem::*;

let p = HttpApiProblem::with_title_and_type_from_status(StatusCode::NOT_FOUND)
    .set_detail("detailed explanation")
    .set_instance("/on/1234/do/something");

assert_eq!(Some("https://httpstatuses.com/404".to_string()), p.type_url);
assert_eq!(Some(StatusCode::NOT_FOUND), p.status);
assert_eq!("Not Found".to_string(), p.title);
assert_eq!(Some("detailed explanation".to_string()), p.detail);
assert_eq!(Some("/on/1234/do/something".to_string()), p.instance);

There is also From<u16> implemented for StatusCode:

use http_api_problem::*;

let p = HttpApiProblem::with_title_and_type_from_status(StatusCode::PRECONDITION_REQUIRED)
    .set_detail("detailed explanation")
    .set_instance("/on/1234/do/something");

assert_eq!(Some("https://httpstatuses.com/428".to_string()), p.type_url);
assert_eq!(Some(StatusCode::PRECONDITION_REQUIRED), p.status);
assert_eq!("Precondition Required".to_string(), p.title);
assert_eq!(Some("detailed explanation".to_string()), p.detail);
assert_eq!(Some("/on/1234/do/something".to_string()), p.instance);

Features

Web Frameworks

There are multiple features to integrate with web frameworks:

  • warp
  • hyper
  • actix-web
  • salvo

These mainly convert the HttpApiProblem to response types of the frameworks and implement traits to integrate with the frameworks error handling

ApiError

The feature api-error enables a structure which can be return from “api handlers” that generate responses and can be converted into an HttpApiProblem.

License

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.

Structs

ApiError

An error that should be returned from an API handler of a web service.

ApiErrorBuilder
HttpApiProblem

Description of a problem that can be returned by an HTTP API based on RFC7807

StatusCode

An HTTP status code (status-code in RFC 7230 et al.).

Statics

PROBLEM_JSON_MEDIA_TYPE

The recommended media type when serialized to JSON

Functions

into_actix_response

HttpApiProblem.

into_hyper_response

Creates an hyper::Response from something that can become an HttpApiProblem.

into_salvo_response

Creates a salvo::Response from something that can become an HttpApiProblem.