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(init)
constructor(init?: HeadersInit)
Creates a new instance of the Headers
class.
init
(optional): An optional parameter that can be an instance ofHeaders
, Web APIHeaders
, an array of key-value pairs, or a record of header fields and values.
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.
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 asthis
when executing the callback function.
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"