Back to Denoland

HTTP Headers

deploy/classic/api/runtime-headers.md

latest2.7 KB
Original Source

:::info Legacy Documentation

You are viewing legacy documentation for Deno Deploy Classic. We recommend migrating to the new <a href="/deploy/">Deno Deploy</a> platform.

:::

The Headers interface is part of the Fetch API. It allows you create and manipulate the HTTP headers of request and response resources of fetch().

Constructor

The Header() constructor creates a new Header instance.

ts
let headers = new Headers(init);

Parameters

nametypeoptionaldescription
initHeaders / { [key: string]: string }trueThe init option lets you initialize the headers object with an existing Headers or an object literal.

The return type of the constructor is a Headers instance.

Methods

namedescription
append(name: string, value: string)Appends a header (overwrites existing one) to the Headers object.
delete(name: string)Deletes a header from the Headers object.
set(name: string, value: string)Create a new header in the Headers object.
get(name: string)Get the value of the header in the Headers object.
has(name: string)Check if the header exists in the Headers objects.
entries()Get the headers as key-value pair. The result is iterable.
keys()Get all the keys of the Headers object. The result is iterable.

Example

ts
// Create a new headers object from an object literal.
const myHeaders = new Headers({
  accept: "application/json",
});

// Append a header to the headers object.
myHeaders.append("user-agent", "Deno Deploy Classic");

// Print the headers of the headers object.
for (const [key, value] of myHeaders.entries()) {
  console.log(key, value);
}

// You can pass the headers instance to Response or Request constructors.
const request = new Request("https://api.github.com/users/denoland", {
  method: "POST",
  headers: myHeaders,
});