# content-type

> Create and parse HTTP Content-Type header

Latest version **3.1.0** (published 2026-09-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install content-type
pnpm add content-type
yarn add content-type
bun add content-type
```

## Health

**Score 75/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; has provenance; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 3.1.0 |
| Published | 2026-09-10 |
| First published | 2013-10-08 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Node | >=22 |
| Dependencies | 0 |
| Unpacked size | 35.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| Provenance | attested (GitHub Actions) |
| GitHub stars | 146 |
| Author | Douglas Christopher Wilson |
| Maintainers | ulisesgascon, blakeembrey, dougwilson |
| Keywords | content-type, http, req, res, rfc7231, rfc9110 |

## Links

- npm: https://www.npmjs.com/package/content-type
- Repository: https://github.com/jshttp/content-type
- Homepage: https://github.com/jshttp/content-type#readme
- Issues: https://github.com/jshttp/content-type/issues
- Funding: https://opencollective.com/express
- npm.io page: https://npm.io/package/content-type

## Alternatives

- [launchdarkly-js-client-sdk](https://npm.io/package/launchdarkly-js-client-sdk.md) — 2.5M weekly downloads
- [@elastic/elasticsearch](https://npm.io/package/@elastic/elasticsearch.md) — 2.1M weekly downloads
- [@c8y/client](https://npm.io/package/@c8y/client.md) — 15.3K weekly downloads
- [@signaldb/maverickjs](https://npm.io/package/@signaldb/maverickjs.md) — 1.7K weekly downloads
- [@bbc/http-transport-cache](https://npm.io/package/@bbc/http-transport-cache.md) — 1.2K weekly downloads

## Recent versions

- 3.1.0 (latest) — 2026-09-10
- 3.0.0 — 2026-08-20
- 2.1.0 — 2026-08-13
- 2.0.0 — 2026-05-11
- 1.0.5 — 2023-01-29
- 1.0.4 — 2017-09-11
- 1.0.3 — 2017-09-11
- 1.0.2 — 2016-05-10
- 1.0.1 — 2015-02-14
- 1.0.0 — 2015-02-02
- 0.0.1 — 2013-10-08

## README

# content-type

[![NPM version][npm-image]][npm-url]
[![NPM downloads][downloads-image]][downloads-url]
[![Build status][build-image]][build-url]
[![Build coverage][coverage-image]][coverage-url]
[![License][license-image]][license-url]

Create and parse HTTP `Content-Type` header.

## Installation

```sh
npm install content-type
```

## API

```js
import * as contentType from "content-type";
```

### contentType.parse(string, options?)

```js
const obj = contentType.parse("image/svg+xml; charset=utf-8");
```

Parse a `Content-Type` header. This will return an object with the following properties (examples are shown for the string `'image/svg+xml; charset=utf-8'`):

- `type`: The media type (always lower case). Example: `'image/svg+xml'`.
- `parameters`: An object of the parameters in the media type (parameter name is always lower case). Example: `{charset: 'utf-8'}`.
- `index`: The index where parsing stopped. Example: `33`.

The parser is lenient and does not validate or throw on malformed input.

#### Options

- `parameters` (default: `true`): Set to `false` to skip parameters.
- `comma` (default: `false`): Set to `true` to stop on a comma. This can be used to parse the media range in an `Accept` header.
- `start` (default: `0`): Set index to start parsing from.

### contentType.format(obj)

```js
const str = contentType.format({
  type: "image/svg+xml",
  parameters: { charset: "utf-8" },
});
```

Format an object into a `Content-Type` header. This will return a string of the content type for the given object with the following properties (examples are shown that produce the string `'image/svg+xml; charset=utf-8'`):

- `type`: The media type. Example: `'image/svg+xml'`.
- `parameters`: An optional object of the parameters in the media type. Example: `{charset: 'utf-8'}`.

Throws a `TypeError` if the object contains an invalid type or parameter names.

### Validation

This package exposes the validation functions used by `format`:

- `isTypeValid` Validates the MIME type against RFC 9110.
- `isTokenValid` Validates a token against RFC 9110 (used for the parameter name).

## License

[MIT](LICENSE)

[npm-image]: https://img.shields.io/npm/v/content-type
[npm-url]: https://npmjs.org/package/content-type
[downloads-image]: https://img.shields.io/npm/dm/content-type
[downloads-url]: https://npmjs.org/package/content-type
[build-image]: https://img.shields.io/github/actions/workflow/status/jshttp/content-type/ci.yml?branch=master
[build-url]: https://github.com/jshttp/content-type/actions/workflows/ci.yml?query=branch%3Amaster
[coverage-image]: https://img.shields.io/codecov/c/gh/jshttp/content-type
[coverage-url]: https://codecov.io/gh/jshttp/content-type
[license-image]: http://img.shields.io/npm/l/content-type.svg?style=flat
[license-url]: LICENSE

---
_Source: https://npm.io/package/content-type · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
