https://github.com/trananhtung/content-type
Parse and format HTTP Content-Type / media-type headers (RFC 9110). A faithful Rust port of the content-type npm package. Zero deps, no_std.
https://github.com/trananhtung/content-type
content-type http media-type no-std rust
Last synced: 5 days ago
JSON representation
Parse and format HTTP Content-Type / media-type headers (RFC 9110). A faithful Rust port of the content-type npm package. Zero deps, no_std.
- Host: GitHub
- URL: https://github.com/trananhtung/content-type
- Owner: trananhtung
- License: apache-2.0
- Created: 2026-06-22T10:51:43.000Z (8 days ago)
- Default Branch: master
- Last Pushed: 2026-06-22T14:45:28.000Z (8 days ago)
- Last Synced: 2026-06-22T16:16:43.284Z (8 days ago)
- Topics: content-type, http, media-type, no-std, rust
- Language: Rust
- Size: 14.6 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE-APACHE
Awesome Lists containing this project
README
# content-type
[](#contributors-)
[](https://crates.io/crates/content-type)
[](https://docs.rs/content-type)
[](https://github.com/trananhtung/content-type/actions/workflows/ci.yml)
[](#license)
**Parse and format HTTP `Content-Type` / media-type headers** (RFC 9110). A faithful
Rust port of the [`content-type`](https://www.npmjs.com/package/content-type) npm
package (v2.0.0): a lenient `parse` and a strict, validating `format`. Zero
dependencies and `#![no_std]`.
```rust
use content_type::{parse, format, ContentType};
let ct = parse("text/html; charset=utf-8");
assert_eq!(ct.type_, "text/html");
assert_eq!(ct.get_parameter("charset"), Some("utf-8"));
let ct = ContentType::new("application/json").with_parameter("charset", "utf-8");
assert_eq!(format(&ct).unwrap(), "application/json; charset=utf-8");
```
## Why content-type?
Reading and writing the `Content-Type` header is a daily HTTP task — pulling out the
media type and `charset`/`boundary`, or building one correctly (quoting parameter
values that need it). This is the lightweight, string-in/string-out port of the
canonical JS implementation. For a richly-typed media-type model, see the `mime`
crate; reach for this when you want the simple `{ type, parameters }` round-trip.
```toml
[dependencies]
content-type = "0.1"
```
## API
| Item | Purpose |
| --- | --- |
| `parse(header)` | Parse into a `ContentType` (lenient, never errors) |
| `parse_with(header, parse_parameters)` | …optionally skipping parameters |
| `format(&content_type)` | Serialize to a header string (validates) |
| `ContentType { type_, parameters }` | `new`, `with_parameter`, `get_parameter` |
## Behavior
- `parse` lower-cases the media type and parameter names; parameter values keep their
case. When a name repeats, the first value wins. Quoted values are unescaped, and
text after a closing quote (up to the next `;`) is ignored.
- `format` validates: the type must be `token/token`, parameter names must be tokens,
and values are emitted bare when they are tokens or quoted (and escaped) otherwise —
returning a `FormatError` if a value can't be represented.
## Differences from the npm package
Two differences exist only for inputs that don't occur in real `Content-Type` headers,
and both stem from JavaScript runtime behavior rather than the package's intent:
- **Parameter order.** Parameters are kept in header order. The JS package returns a
plain object, and V8 iterates *integer-like* keys (e.g. `5`, `1`) first in ascending
order ahead of other keys — so for the (unheard-of) case of numeric parameter names,
iteration/format order can differ. The set of `(name, value)` pairs is always
identical (first occurrence wins).
- **Unicode lower-casing.** The media type and parameter names are lower-cased with
Rust's Unicode lower-casing, which can differ from JS for context-sensitive cases
(Greek final sigma) in non-ASCII names. Standard ASCII headers are unaffected.
## Contributors ✨
This project follows the [all-contributors](https://github.com/all-contributors/all-contributors) specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the [emoji key](https://allcontributors.org/docs/en/emoji-key) for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
## License
Licensed under either of [Apache-2.0](LICENSE-APACHE) or [MIT](LICENSE-MIT) at
your option.