Headers

The Headers class represents a set of HTTP headers and provides methods and properties for working with them. It is very similar to the standard Headers class used in web APIs.


Constructor

constructor(init)

constructor(init?: HeadersInit) Creates a new instance of the Headers class.

  • init (optional): An optional parameter that can be an instance of Headers, Web API Headers, an array of key-value pairs, or a record of header fields and values.

Methods

append(name, value)

append(name: string, value: string): void
Appends a new value onto an existing header's value, or adds the header if it does not already exist.

  • name - The name of the header.
  • value - The value to append to the header.

has(name)

has(name: string): boolean
Checks if a header with a specified name exists.

  • name - The name of the header to check.

get(name)

get(name: string): string | null
Returns the first value of the specified header.

  • name - The name of the header.

set(name, value)

set(name: string, value: string): void
Sets a new value for an existing header, or adds the header if it does not already exist.

  • name - The name of the header.
  • value - The new value to set for the header.

delete(name)

delete(name: string): void
Removes a header and its value from the set of headers.

  • name - The name of the header to delete.

toJSON()

toJSON(): Record<string, string>
Returns a JSON representation of the headers.


toString()

toString(): string
Returns a string representation of the headers in JSON format.


getSetCookie()

getSetCookie(): string[]
Returns an array of values of the 'Set-Cookie' header.


forEach(callbackFn)

forEach(callbackfn: (value: string, key: string, iterable: Headers) => void): void
Executes a provided function once per each header present in the set of headers.

  • callbackfn - A function to execute for each header.
  • thisArg - An optional value to use as this when executing the callback function.

keys()

keys(): IterableIterator<string>
Returns an iterator of all the header names.


values()

values(): IterableIterator<string>
Returns an iterator of all the header values.


entries()

entries(): IterableIterator<[string, string]>
Returns an iterator of all the header names and values as key-value pairs.


Examples

The following examples, illustrate how to construct a Headers class, append and retreive values from the object.

import { Headers } from "sherpa-core";
const _headers = new Headers();

_headers.append("Content-Type", "application/json");
_headers.get("Content-Type"); // returns "application/json"