or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

index.md
tile.json

index.mddocs/

Content-Type

Content-Type is a lightweight, RFC 7231-compliant Node.js library for parsing and formatting HTTP Content-Type headers. It provides comprehensive functionality for parsing Content-Type headers from strings, HTTP requests, and responses, extracting media types and parameters with proper handling of quoted strings and special characters.

Package Information

  • Package Name: content-type
  • Package Type: npm
  • Language: JavaScript
  • Installation: npm install content-type

Core Imports

var contentType = require('content-type');

For ES modules:

import * as contentType from 'content-type';

Basic Usage

var contentType = require('content-type');

// Parse a Content-Type header string
var parsed = contentType.parse('text/html; charset=utf-8');
console.log(parsed.type);       // 'text/html'
console.log(parsed.parameters); // { charset: 'utf-8' }

// Format an object to Content-Type header
var header = contentType.format({
  type: 'text/html',
  parameters: { charset: 'utf-8' }
});
console.log(header); // 'text/html; charset=utf-8'

// Parse from HTTP request object
var parsed = contentType.parse(req);

// Parse from HTTP response object  
var parsed = contentType.parse(res);

Capabilities

Content-Type Parsing

Parse Content-Type headers from strings or HTTP request/response objects into structured objects.

/**
 * Parse a Content-Type header string into an object
 * @param {string} string - Content-Type header string to parse
 * @returns {ContentType} Parsed Content-Type object
 * @throws {TypeError} If string is missing or invalid
 */
contentType.parse(string);

/**
 * Parse Content-Type header from HTTP request object
 * @param {object} req - HTTP request-like object with headers property
 * @returns {ContentType} Parsed Content-Type object  
 * @throws {TypeError} If Content-Type header is missing or invalid
 */
contentType.parse(req);

/**
 * Parse Content-Type header from HTTP response object
 * @param {object} res - HTTP response-like object with getHeader method
 * @returns {ContentType} Parsed Content-Type object
 * @throws {TypeError} If Content-Type header is missing or invalid
 */
contentType.parse(res);

Usage Examples:

var contentType = require('content-type');

// Parse basic media type
var type = contentType.parse('text/html');
console.log(type.type);       // 'text/html'
console.log(type.parameters); // {}

// Parse with parameters
var type = contentType.parse('text/html; charset=utf-8; boundary=something');
console.log(type.type);       // 'text/html'
console.log(type.parameters); // { charset: 'utf-8', boundary: 'something' }

// Parse with quoted parameters
var type = contentType.parse('text/html; charset="utf-8"; name="file name.txt"');
console.log(type.parameters); // { charset: 'utf-8', name: 'file name.txt' }

// Parse from HTTP request
var type = contentType.parse(req);

// Parse from HTTP response
var type = contentType.parse(res);

Content-Type Formatting

Format Content-Type objects into valid RFC 7231-compliant header strings.

/**
 * Format an object into a Content-Type header string
 * @param {object} obj - Object with type and optional parameters properties
 * @param {string} obj.type - The media type (e.g., 'text/html')
 * @param {object} [obj.parameters] - Optional parameters object
 * @returns {string} Formatted Content-Type header string
 * @throws {TypeError} If object contains invalid type or parameter names
 */
contentType.format(obj);

Usage Examples:

var contentType = require('content-type');

// Format basic type
var header = contentType.format({ type: 'text/html' });
console.log(header); // 'text/html'

// Format with parameters
var header = contentType.format({
  type: 'text/html',
  parameters: { charset: 'utf-8' }
});
console.log(header); // 'text/html; charset=utf-8'

// Format with multiple parameters (sorted alphabetically)
var header = contentType.format({
  type: 'text/html',
  parameters: { charset: 'utf-8', boundary: 'something', foo: 'bar' }
});
console.log(header); // 'text/html; boundary=something; charset=utf-8; foo=bar'

// Format with parameters that need quoting
var header = contentType.format({
  type: 'text/html',
  parameters: { filename: 'document with spaces.pdf' }
});
console.log(header); // 'text/html; filename="document with spaces.pdf"'

Types

/**
 * ContentType object returned by parse operations
 * @typedef {object} ContentType
 * @property {string} type - The media type in lowercase (e.g., 'text/html')
 * @property {object} parameters - Object with parameter name/value pairs (names in lowercase)
 */

/**
 * Input object for format operations
 * @typedef {object} FormatObject
 * @property {string} type - The media type (will be validated)
 * @property {object} [parameters] - Optional parameters object with string values
 */

Error Handling

All functions throw TypeError with descriptive messages for invalid inputs:

  • Missing arguments: "argument string is required", "argument obj is required"
  • Invalid types: "invalid media type", "invalid type"
  • Invalid parameters: "invalid parameter name", "invalid parameter value", "invalid parameter format"
  • Missing headers: "content-type header is missing from object"
  • Type validation: "argument string is required to be a string"

Error Examples:

var contentType = require('content-type');

try {
  contentType.parse('invalid/');
} catch (err) {
  console.log(err.message); // 'invalid media type'
}

try {
  contentType.format({});
} catch (err) {
  console.log(err.message); // 'argument obj is required'
}

try {
  contentType.format({ type: 'invalid type' });
} catch (err) {
  console.log(err.message); // 'invalid type'
}