Parse Content-Type Header Strings

This package will parse the {Content-Type} header field into an introspectable data structure, whose parameters can be manipulated:

const contentTypeParser = require("content-type-parser");

const contentType = contentTypeParser(`Text/HTML;Charset="utf-8"`);

console.assert(contentType.toString() === "text/html;charset=utf-8");

console.assert(contentType.type === "text");
console.assert(contentType.subtype === "html");
console.assert(contentType.get("charset") === "utf-8");

contentType.set("charset", "windows-1252");
console.assert(contentType.get("charset") === "windows-1252");
console.assert(contentType.toString() === "text/html;charset=windows-1252");

console.assert(contentType.isHTML() === true);
console.assert(contentType.isXML() === false);
console.assert(contentType.isText() === true);

Note how parsing will lowercase the type, subtype, and parameter name tokens (but not parameter values).

If the passed string cannot be parsed as a content-type, contentTypeParser will return null.

ContentType instance API

This package's main module's default export will return an instance of the ContentType class, which has the following public APIs:

Properties

Parameter manipulation

In general you should not directly manipulate parameterList. Instead, use the following APIs:

Both of these will lowercase the keys.

MIME type tests

Serialization

Credits

This package was originally based on the excellent work of @nicolashenry, in jsdom. It has since been pulled out into this separate package.