Parses HTTP Content-Type values as specified in https://tools.ietf.org/html/rfc7231.
Although Content-Disposition headers (RFC6266) are very similar to Content-Type headers, they are subtly different in the treatment of white-space. Additionally, this library does not support extended character notation (filename*=...
). Therefore this library is not recommended for Content-Disposition headers.
const ContentType = require( 'content-type-header' );
Parses content-type headers and formats them to a string.
header
The string header value to be parsed. This should be trimmed of white-space.
Create a new ContentType object.
let contentType = new ContentType( 'text/html; charset=utf-8; foo="bar fee"' );
// This can also be done via a static method, see below
contentType = ContentType.parse( 'text/html; charset=utf-8; foo="bar fee"' );
The object has the following properties:
type
The content-type type, egtext
subtype
The content-type subtype, eghtml
mediaType
The full content-type, egtext/html
charset
The charset, egutf-8
parameters
Any parameters, including the charset, eg{ charset: 'utf-8', foo: 'bar fee' }
Any updates to mediaType
will also update and type and subtype. Any updates to charset will also update parameters.
If the format does not match the RFC7231 specification an error will be thrown. This is deliberate. Incorrect media type identification can lead to privilege in certain contexts; therefore, it is better to disregard badly formatted content-type headers.
Parse a content-type header and update the properties listed above.
Format the contentType into a string.
const contentType = new ContentType();
contentType.type = 'image';
contentType.subtype = 'gif';
contentType.format(); // => 'image/gif'
Parse a content-type string and return a ContentType object. If passed an existing contentType instance then this will be passed though.
Format an object, which can be a ContentType object or a plain object, to a content type string.
If a plain object is used, type
and subtype
will be used in preference to mediaType
, and charset
will be used in preference to parameters
.
ContentType.format( { mediaType: 'text/html', charset: 'utf-8' } )
// => 'text/html; charset=utf-8'