Table of Contents

Class Headers

Namespace
CSharpToJavaScript.APIs.JS
Assembly
CSharpToJavaScript.dll

The Headers interface of the Fetch API allows you to perform various actions on HTTP request and response headers. These actions include retrieving, setting, adding to, and removing headers from the list of the request's headers.

[Value("Headers")]
public class Headers
Inheritance
Headers
Inherited Members

Remarks

You can retrieve a Headers object via the Headers and Headers properties, and create a new Headers object using the Headers(Union39) constructor. Compared to using plain objects, using Headers objects to send requests provides some additional input sanitization. For example, it normalizes header names to lowercase, strips leading and trailing whitespace from header values, and prevents certain headers from being set.

NOTE

You can find out more about the available headers by reading our HTTP headers reference.

-ServiceWorker API
-HTTP access control (CORS)
-HTTP

See also on MDN

Constructors

Headers()

public Headers()

Headers(Union39)

The Headers() constructor creates a new
Headers object.

public Headers(Union39 init = default)

Parameters

init Union39

Remarks

Properties

this[int]

public string this[int i] { get; set; }

Parameters

i int

Property Value

string

Methods

Append(string, string)

The append() method of the Headers
interface appends a new value onto an existing header inside a Headers
object, or adds the header if it does not already exist.

[Value("append")]
public GlobalObject.Undefined Append(string name, string value)

Parameters

name string
value string

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

The difference between Set(string, string) and append() is
that if the specified header already exists and accepts multiple values,
set() will overwrite the existing value with the new one, whereas
append() will append the new value onto the end of the set of values.

For security reasons, some headers can only be controlled by the user agent. These
headers include the Forbidden_request_header
and Forbidden_response_header_name.

-ServiceWorker API
-HTTP access control (CORS)
-HTTP

See also on MDN

Delete(string)

The delete() method of the Headers
interface deletes a header from the current Headers object.

[Value("delete")]
public GlobalObject.Undefined Delete(string name)

Parameters

name string

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

For security reasons, some headers can only be controlled by the user agent. These
headers include the Forbidden_request_header
and Forbidden_response_header_name.

-ServiceWorker API
-HTTP access control (CORS)
-HTTP

See also on MDN

Get(string)

The get() method of the Headers interface
returns a byte string of all the values of a header within a Headers object
with a given name. If the requested header doesn't exist in the Headers
object, it returns null.

[Value("get")]
public string? Get(string name)

Parameters

name string

Returns

string

A String sequence representing the values of the retrieved header or
null if this header is not set.

Remarks

For security reasons, some headers can only be controlled by the user agent. These
headers include the Forbidden_request_header
and Forbidden_response_header_name.

-ServiceWorker API
-HTTP access control (CORS)
-HTTP

See also on MDN

GetSetCookie()

The getSetCookie() method of the Headers interface returns an array containing the values of all {{httpheader("Set-Cookie")}} headers associated with a response. This allows Headers objects to handle having multiple Set-Cookie headers, which wasn't possible prior to its implementation.

[Value("getSetCookie")]
public List<string> GetSetCookie()

Returns

List<string>

An array of strings representing the values of all the different Set-Cookie headers associated with a response.If no Set-Cookie headers are set, the method will return an empty array ([ ]).

Remarks

This method is intended for use on server environments (for example Node.js). Browsers block frontend JavaScript code from accessing the Set-Cookie header, as required by the Fetch spec, which defines Set-Cookie as a forbidden response-header name that must be filtered out from any response exposed to frontend code.

-HTTP
-Set-Cookie

See also on MDN

Has(string)

The has() method of the Headers interface
returns a boolean stating whether a Headers object contains a certain
header.

[Value("has")]
public bool Has(string name)

Parameters

name string

Returns

bool

A boolean value.

Remarks

For security reasons, some headers can only be controlled by the user agent. These
headers include the Forbidden_request_header
and Forbidden_response_header_name.

-ServiceWorker API
-HTTP access control (CORS)
-HTTP

See also on MDN

Set(string, string)

The set() method of the Headers interface
sets a new value for an existing header inside a Headers object, or adds
the header if it does not already exist.

[Value("set")]
public GlobalObject.Undefined Set(string name, string value)

Parameters

name string
value string

Returns

GlobalObject.Undefined

None (GlobalObject.Undefined).

Remarks

The difference between set() and Append(string, string) is that if
the specified header already exists and accepts multiple values, set()
overwrites the existing value with the new one, whereas Append(string, string)
appends the new value to the end of the set of values.

For security reasons, some headers can only be controlled by the user agent. These
headers include the Forbidden_request_header
and Forbidden_response_header_name.

-ServiceWorker API
-HTTP access control (CORS)
-HTTP

See also on MDN