
Security News
White House Authorizes Private Companies to Conduct Offensive Cyber Operations
A new federal program will let vetted U.S. cybersecurity firms help investigate and disrupt foreign cybercrime groups under government direction.
content-type
Advanced tools
Create and parse HTTP Content-Type header.
npm install content-type
const contentType = require("content-type");
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. Example: 'image/svg+xml'.parameters: An object of the parameters in the media type (parameter name is always lower case). Example: {charset: 'utf-8'}.The parser is lenient and does not error. You should validate type and parameters before trusting them.
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.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.
The mime-types package is similar to content-type as it provides the ability to look up the content-type based on a file extension and vice versa. It is more focused on the association between file extensions and mime types rather than parsing and formatting Content-Type headers.
The mime package is another alternative that allows you to map between MIME types and file extensions. It provides functionality to define custom mime type mappings and is more comprehensive in terms of the number of file types it supports compared to content-type.
Negotiator is an HTTP content negotiation library that goes beyond just handling Content-Type headers. It can be used to negotiate language, charset, encoding, etc., based on the Accept-* headers from the client. It is more complex and offers broader functionality for handling HTTP negotiations.
FAQs
Create and parse HTTP Content-Type header
The npm package content-type receives a total of 119,931,564 weekly downloads. As such, content-type popularity was classified as popular.
We found that content-type demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 3 open source maintainers collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
A new federal program will let vetted U.S. cybersecurity firms help investigate and disrupt foreign cybercrime groups under government direction.

Research
/Security News
The campaign amassed more than 75,000 installs by targeting Russian-speaking users seeking access to blocked services.

Company News
Open source maintainers are under more pressure than ever. We're raising our open source program from the Team plan to the Business plan, free.