Crate http_api_problem [] [src]

HTTP-API-PROBLEM

crates.io docs.rs downloads Build Status license-mit license-apache

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

This library depends on serde.

The HttpApiProblem struct implements Serialize and Deserialize.

Usage

Add this to your Cargo.toml:

http-api-problem = "0.2"

Add this crate root:

extern crate http_api_problem;

Features

To directly construct from [iron](http://ironframework.io/)::StatusCode the feature iron implements From<iron::status::StatusCode> for HttpStatusCode.

Examples

//! ```rust use http_api_problem::*;

let p = HttpApiProblem::with_title_and_type_from_status(HttpStatusCode::NotFound) .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(404), 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 HttpStatusCode:

use http_api_problem::*;

let p =
    HttpApiProblem::with_title_and_type_from_status(428)
    .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(428), 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);

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

HttpApiProblem

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

Enums

HttpStatusCode

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

Statics

PROBLEM_JSON_MEDIA_TYPE

The recommended media type when serialized to JSON

PROBLEM_XML_MEDIA_TYPE

The recommended media type when serialized to XML