#http-header #header #http #content-negotiation

accept-header

A simple library for parsing HTTP Accept headers for content negotiation

4 releases

0.2.3 Feb 14, 2023
0.2.2 Jan 11, 2023
0.2.1 Nov 27, 2022
0.1.2 Nov 14, 2022

#65 in #http-header

Download history 33/week @ 2024-06-24 41/week @ 2024-07-01 52/week @ 2024-07-08 24/week @ 2024-07-15 63/week @ 2024-07-22 38/week @ 2024-07-29 21/week @ 2024-08-05 27/week @ 2024-08-12 21/week @ 2024-08-19 29/week @ 2024-08-26 13/week @ 2024-09-02 23/week @ 2024-09-09 41/week @ 2024-09-16 230/week @ 2024-09-23 143/week @ 2024-09-30 724/week @ 2024-10-07

1,140 downloads per month
Used in 2 crates

MIT license

14KB
309 lines

HTTP accept header

A very basic & naive implementation of the HTTP Accept header. It uses http crate and mime crate to parse the accept header. Basic data structure:

#[derive(Debug, Clone, PartialEq)]
pub struct Accept {
    pub wildcard: Option<MediaType>,
    pub types: Vec<MediaType>,
}

#[derive(Debug, Clone, PartialEq)]
pub struct MediaType {
    pub mime: Mime,
    pub weight: Option<f32>,
}

Usage:

// parse accept header
let accept: Accept = "application/json, text/html;q=0.9, text/plain;q=0.8, */*;q=0.7"
    .parse()
    .unwrap();

// prepare a list of supported media types
let available = vec![
    Mime::from_str("text/html").unwrap(),
    Mime::from_str("application/json").unwrap(),
];

// content negotiation
let negotiated = accept.negotiate(&available).unwrap();

// "application/json" shall be chosen since it is available and has the highest weight
assert_eq!(negotiated, Mime::from_str("application/json").unwrap());

Dependencies

~2.5MB
~57K SLoC