Skip to content

contentHeader

Dmytro Katashev edited this page Aug 12, 2024 · 1 revision

contentHeader API Reference

Overview

The contentHeader utility provides functions for parsing and formatting content headers, such as Content-Type and Content-Disposition, within Salesforce Commerce Cloud (SFCC). This utility is essential for services that need to handle MIME types and content disposition parameters in HTTP headers.


Public Methods

parse(header: string, headerType?: 'type' | 'disposition')

Description: Parses a content header string into an object with a type and associated parameters.

  • Parameters:
    • header (string): The content header string to parse.
    • headerType ('type' | 'disposition', optional): The type of content header being parsed. Defaults to 'type'.
  • Returns:
    • ContentHeader: The parsed content header information.

Usage Example:

var contentHeader = require('*/cartridge/scripts/util/contentHeader');

var header = 'multipart/form-data; boundary=--Boundary';
var parsedHeader = contentHeader.parse(header);

console.log(parsedHeader.type); // 'multipart/form-data'
console.log(parsedHeader.params.boundary); // '--Boundary'

format(contentHeader: ContentHeader)

Description: Creates a content header string from a ContentHeader object.

  • Parameters:
    • contentHeader (ContentHeader): The content header information.
  • Returns:
    • string: The formatted content header string.

Usage Example:

var contentHeader = require('*/cartridge/scripts/util/contentHeader');

var headerObject = {
    type: 'multipart/form-data',
    params: {
        boundary: '--Boundary'
    }
};
var formattedHeader = contentHeader.format(headerObject);

console.log(formattedHeader); // 'multipart/form-data; boundary=--Boundary'

Types

ContentHeader

Represents content type information.

  • Type: Object
    • type (string): The MIME/Disposition type.
    • params (Object.<string,string>): The parameters associated with the MIME/Disposition type.

Usage Example:

var contentHeader = {
    type: 'multipart/form-data',
    params: {
        boundary: '--Boundary'
    }
};

Regular Expressions

MIME_TYPE_RE

Description: Regular expression for validating MIME types.

  • Pattern: /^[!#$%&'*+.^\w|~-]+\/[!#$%&'*+.^\w|~-]+$/

DISPOSITION_TYPE_RE

Description: Regular expression for validating disposition types.

  • Pattern: /^[!#$%&'*+.^\w|~-]+$/

PARAMETER_RE

Description: Regular expression for validating and parsing parameters in the content header.

  • Pattern: / *([!#$%&'*+.^\w|~-]+)=("(?:[\x0b\x20\x21\x23-\x5b\x5d-\x7e\x80-\xff]|\[\x0b\x20-\xff])"|[!#$%&'+.^\w|~-]+) */

Clone this wiki locally