
Security News
ECMAScript 2025 Finalized with Iterator Helpers, Set Methods, RegExp.escape, and More
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
headers-polyfill
Advanced tools
The headers-polyfill npm package provides a way to manipulate HTTP headers, allowing you to create, read, and modify HTTP headers in a standardized manner. It is designed to mimic the Headers interface provided by the Fetch API, making it useful for environments where this API is not available or for projects that aim for consistency across different environments.
Creating and appending headers
This feature allows you to create a new Headers object and append new headers to it. It's useful for setting up the headers required for an HTTP request.
{"const headers = new Headers();\nheaders.append('Content-Type', 'application/json');\nheaders.append('Authorization', 'Bearer your_token_here');"}
Reading header values
This feature enables you to read the value of a specific header by name. It's useful for inspecting headers received in an HTTP response.
{"const contentType = headers.get('Content-Type');"}
Checking for a header's existence
This feature allows you to check if a certain header exists within the Headers object. It's useful for conditional logic based on the presence of specific headers.
{"const hasContentType = headers.has('Content-Type');"}
Deleting a header
This feature enables you to remove a header from the Headers object. It's useful for modifying headers before sending an HTTP request or after processing an HTTP response.
{"headers.delete('Authorization');"}
Similar to headers-polyfill, fetch-headers provides an implementation of the Fetch API's Headers interface. It allows for manipulation of HTTP headers in environments where the Fetch API is not natively supported. The main difference lies in the specific implementation details and possibly the extent of compatibility with the Fetch API specification.
While primarily known for bringing window.fetch to Node.js, node-fetch also includes an implementation of the Headers interface. Compared to headers-polyfill, node-fetch offers a more comprehensive solution by also providing the ability to perform HTTP requests, making it a more feature-rich package for projects that require both fetching capabilities and headers manipulation.
headers-polyfill
A Headers
class polyfill and transformation library.
Various request issuing libraries utilize a different format of headers. This library chooses the Headers
instance as the middle-ground between server and client, and provides functions to convert that instance to primitives and vice-versa.
npm install headers-polyfill
This package exports the Headers
class that polyfills the native window.Headers
implementation. This allows you to construct and manage headers using the same API in non-browser environments.
import { Headers } from 'headers-polyfill'
const headers = new Headers({
Accept: '*/*',
'Content-Type': 'application/json',
})
headers.get('accept') // "*/*"
The Headers
polyfill instance supports the same methods as the standard Headers
instance:
As well as the iterator methods:
getRawHeaders()
Returns an object consisting of the header name/value pairs but preserving raw header names.
const headers = new Headers({
Accept: '*/*',
'Content-Type': 'application/json',
})
headers.raw()
// { "Accept": "*/*", "Content-Type": "application/json" }
headersToString: (h: Headers): string
import { headersToString } from 'headers-polyfill'
headersToString(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// connetion: keep-alive
// content-type: text/plain, image/png
headersToList: (h: Headers): Array<[string, string | string[]]>
import { headersToList } from 'headers-polyfill'
headersToList(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// [['connection', 'keep-alive'], ['content-type', ['text/plain', 'image/png']]]
headersToObject: (h: Headers): Record<string, string | string[]>
import { headersToObject } from 'headers-polyfill'
headersToObject(
new Headers({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
)
// { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
stringToHeaders: (s: string): Headers
import { stringToHeaders } from 'headers-polyfill'
const stringToHeaders(`
connection: keep-alive
content-type: text/plain, image/png
`)
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
listToHeaders: (l: Array<[string, string | string[]]>): Headers
import { listToHeaders } from 'headers-polyfill'
listToHeaders([
['connection', 'keep-alive'],
['content-type', ['text/plain', 'image/png']],
])
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
objectToHeaders: (o: Record<string, string | string[] | undefined>): Headers
import { objectToHeaders } from 'headers-polyfill'
objectToHeaders({
connection: 'keep-alive',
'content-type': ['text/plain', 'image/png'],
})
// Headers { connection: 'keep-alive', 'content-type': ['text/plain', 'image/png'] }
reduceHeadersObject: <R>(o: Record<string, string | string[]>, reducer: (acc: R, name: string, value: string | string[]) => R) => R
import { reduceHeadersObject } from 'headers-polyfill'
reduceHeadersObject <
HeadersObject >
({
Accept: '*/*',
'Content-Type': ['application/json', 'text/plain'],
},
(headers, name, value) => {
headers[name.toLowerCase()] = value
return headers
},
{})
// { 'accept': '*/*', 'content-type': ['application/json', 'text/plain'] }
appendHeader: (o: Record<string, string | string[]>, n: string, v: string | string[]): Record<string, string | string[]>
import { appendHeader } from 'headers-polyfill'
appendHeader(
{ 'content-type': 'application/json' },
'content-type',
'text/plain'
)
// { 'content-type': ['application/json', 'text/plain']}
flattenHeadersList: (l: Array<[string, string | string[]]>): Array<string, string>
import { flattenHeadersList } from 'headers-polyfill'
flattenHeadersList([['content-type', ['text/plain', 'image/png']]])
// ['content-type', 'text/plain, image/png']
flattenHeadersObject: (o: Record<string, string | string[]>): Record<string, string>
import { flattenHeadersObject } from 'headers-polyfill'
flattenHeadersObject({
'content-type': ['text/plain', 'image/png'],
})
// { 'content-type': 'text/plain, image/png' }
FAQs
A native "Headers" class polyfill.
The npm package headers-polyfill receives a total of 3,199,151 weekly downloads. As such, headers-polyfill popularity was classified as popular.
We found that headers-polyfill demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer 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
ECMAScript 2025 introduces Iterator Helpers, Set methods, JSON modules, and more in its latest spec update approved by Ecma in June 2025.
Security News
A new Node.js homepage button linking to paid support for EOL versions has sparked a heated discussion among contributors and the wider community.
Research
North Korean threat actors linked to the Contagious Interview campaign return with 35 new malicious npm packages using a stealthy multi-stage malware loader.