Skip to content

HttpHeaders abstract interface

abstract interface class HttpHeaders

Headers for HTTP requests and responses.

In some situations, headers are immutable:

In these situations, the mutating methods throw exceptions.

For all operations on HTTP headers the header name is case-insensitive.

To set the value of a header use the set() method:

dart
request.headers.set(HttpHeaders.cacheControlHeader,
                    'max-age=3600, must-revalidate');

To retrieve the value of a header use the value() method:

dart
print(request.headers.value(HttpHeaders.userAgentHeader));

An HttpHeaders object holds a list of values for each name as the standard allows. In most cases a name holds only a single value, The most common mode of operation is to use set() for setting a value, and value() for retrieving a value.

Properties

chunkedTransferEncoding read / write

bool chunkedTransferEncoding

getter:

Whether the connection uses chunked transfer encoding.

Reflects and modifies the value of the transferEncodingHeader header.

setter:

Whether the connection uses chunked transfer encoding.

Reflects and modifies the value of the transferEncodingHeader header.

Implementation
dart
late bool chunkedTransferEncoding;

contentLength read / write

int contentLength

getter:

The value of the contentLengthHeader header, if any.

The value is negative if there is no content length set.

setter:

The value of the contentLengthHeader header, if any.

The value is negative if there is no content length set.

Implementation
dart
int contentLength = -1;

contentType read / write

ContentType? contentType

getter:

The ContentType of the contentTypeHeader header, if any.

setter:

The ContentType of the contentTypeHeader header, if any.

Implementation
dart
ContentType? contentType;

date read / write

DateTime? date

getter:

The date specified by the dateHeader header, if any.

setter:

The date specified by the dateHeader header, if any.

Implementation
dart
DateTime? date;

expires read / write

DateTime? expires

getter:

The date and time specified by the expiresHeader header, if any.

setter:

The date and time specified by the expiresHeader header, if any.

Implementation
dart
DateTime? expires;

hashCode no setter inherited

int get hashCode

The hash code for this object.

A hash code is a single integer which represents the state of the object that affects operator == comparisons.

All objects have hash codes. The default hash code implemented by Object represents only the identity of the object, the same way as the default operator == implementation only considers objects equal if they are identical (see identityHashCode).

If operator == is overridden to use the object state instead, the hash code must also be changed to represent that state, otherwise the object cannot be used in hash based data structures like the default Set and Map implementations.

Hash codes must be the same for objects that are equal to each other according to operator ==. The hash code of an object should only change if the object changes in a way that affects equality. There are no further requirements for the hash codes. They need not be consistent between executions of the same program and there are no distribution guarantees.

Objects that are not equal are allowed to have the same hash code. It is even technically allowed that all instances have the same hash code, but if clashes happen too often, it may reduce the efficiency of hash-based data structures like HashSet or HashMap.

If a subclass overrides hashCode, it should override the operator == operator as well to maintain consistency.

Inherited from Object.

Implementation
dart
external int get hashCode;

host read / write

String? host

getter:

The value of the hostHeader header, if any.

setter:

The value of the hostHeader header, if any.

Implementation
dart
String? host;

ifModifiedSince read / write

DateTime? ifModifiedSince

getter:

The date and time specified by the ifModifiedSinceHeader header, if any.

setter:

The date and time specified by the ifModifiedSinceHeader header, if any.

Implementation
dart
DateTime? ifModifiedSince;

persistentConnection read / write

bool persistentConnection

getter:

Whether the connection is persistent (keep-alive).

setter:

Whether the connection is persistent (keep-alive).

Implementation
dart
late bool persistentConnection;

port read / write

int? port

getter:

The value of the port part of the hostHeader header, if any.

setter:

The value of the port part of the hostHeader header, if any.

Implementation
dart
int? port;

runtimeType no setter inherited

Type get runtimeType

A representation of the runtime type of the object.

Inherited from Object.

Implementation
dart
external Type get runtimeType;

Methods

add()

void add(String name, Object value, {bool preserveHeaderCase = false})

Adds a header value.

The header named name will have a string value derived from value added to its list of values.

Some headers are single valued, and for these, adding a value will replace a previous value. If the value is a DateTime, an HTTP date format will be applied. If the value is an Iterable, each element will be added separately. For all other types the default Object.toString method will be used.

Header names are converted to lower-case unless preserveHeaderCase is set to true. If two header names are the same when converted to lower-case, they are considered to be the same header, with one set of values.

The current case of the a header name is that of the name used by the last set or add call for that header.

Implementation
dart
void add(String name, Object value, {bool preserveHeaderCase = false});

clear()

void clear()

Removes all headers.

Some headers have system supplied values which cannot be removed. All other header values are removed, and header names with not remaining values are no longer considered present.

Implementation
dart
void clear();

forEach()

void forEach(void Function(String name, List<String> values) action)

Performs the action on each header.

The action function is called with each header's name and a list of the header's values. The casing of the name string is determined by the last add or set operation for that particular header, which defaults to lower-casing the header name unless explicitly set to preserve the case.

Implementation
dart
void forEach(void Function(String name, List<String> values) action);

noFolding()

void noFolding(String name)

Disables folding for the header named name when sending the HTTP header.

By default, multiple header values are folded into a single header line by separating the values with commas.

The 'set-cookie' header has folding disabled by default.

Implementation
dart
void noFolding(String name);

noSuchMethod() inherited

dynamic noSuchMethod(Invocation invocation)

Invoked when a nonexistent method or property is accessed.

A dynamic member invocation can attempt to call a member which doesn't exist on the receiving object. Example:

dart
dynamic object = 1;
object.add(42); // Statically allowed, run-time error

This invalid code will invoke the noSuchMethod method of the integer 1 with an Invocation representing the .add(42) call and arguments (which then throws).

Classes can override noSuchMethod to provide custom behavior for such invalid dynamic invocations.

A class with a non-default noSuchMethod invocation can also omit implementations for members of its interface. Example:

dart
class MockList<T> implements List<T> {
  noSuchMethod(Invocation invocation) {
    log(invocation);
    super.noSuchMethod(invocation); // Will throw.
  }
}
void main() {
  MockList().add(42);
}

This code has no compile-time warnings or errors even though the MockList class has no concrete implementation of any of the List interface methods. Calls to List methods are forwarded to noSuchMethod, so this code will log an invocation similar to Invocation.method(#add, [42]) and then throw.

If a value is returned from noSuchMethod, it becomes the result of the original invocation. If the value is not of a type that can be returned by the original invocation, a type error occurs at the invocation.

The default behavior is to throw a NoSuchMethodError.

Inherited from Object.

Implementation
dart
@pragma("vm:entry-point")
@pragma("wasm:entry-point")
external dynamic noSuchMethod(Invocation invocation);

remove()

void remove(String name, Object value)

Removes a specific value for a header name.

Some headers have system supplied values which cannot be removed. For all other headers and values, the value is converted to a string in the same way as for add, then that string value is removed from the current values of name. If there are no remaining values for name, the header is no longer considered present.

Implementation
dart
void remove(String name, Object value);

removeAll()

void removeAll(String name)

Removes all values for the specified header name.

Some headers have system supplied values which cannot be removed. All other values for name are removed. If there are no remaining values for name, the header is no longer considered present.

Implementation
dart
void removeAll(String name);

set()

void set(String name, Object value, {bool preserveHeaderCase = false})

Sets the header name to value.

Removes all existing values for the header named name and then adds value to it.

Implementation
dart
void set(String name, Object value, {bool preserveHeaderCase = false});

toString() inherited

String toString()

A string representation of this object.

Some classes have a default textual representation, often paired with a static parse function (like int.parse). These classes will provide the textual representation as their string representation.

Other classes have no meaningful textual representation that a program will care about. Such classes will typically override toString to provide useful information when inspecting the object, mainly for debugging or logging.

Inherited from Object.

Implementation
dart
external String toString();

value()

String? value(String name)

Convenience method for the value for a single valued header.

The value must not have more than one value.

Returns null if there is no header with the provided name.

Implementation
dart
String? value(String name);

Operators

operator ==() inherited

bool operator ==(Object other)

The equality operator.

The default behavior for all Objects is to return true if and only if this object and other are the same object.

Override this method to specify a different equality relation on a class. The overriding method must still be an equivalence relation. That is, it must be:

  • Total: It must return a boolean for all arguments. It should never throw.

  • Reflexive: For all objects o, o == o must be true.

  • Symmetric: For all objects o1 and o2, o1 == o2 and o2 == o1 must either both be true, or both be false.

  • Transitive: For all objects o1, o2, and o3, if o1 == o2 and o2 == o3 are true, then o1 == o3 must be true.

The method should also be consistent over time, so whether two objects are equal should only change if at least one of the objects was modified.

If a subclass overrides the equality operator, it should override the hashCode method as well to maintain consistency.

Inherited from Object.

Implementation
dart
external bool operator ==(Object other);

operator

List<String>? operator [](String name)

The values for the header named name.

Returns null if there is no header with the provided name, otherwise returns a new list containing the current values. Not that modifying the list does not change the header.

Implementation
dart
List<String>? operator [](String name);

Constants

acceptCharsetHeader

const String acceptCharsetHeader
Implementation
dart
static const acceptCharsetHeader = "accept-charset";

acceptEncodingHeader

const String acceptEncodingHeader
Implementation
dart
static const acceptEncodingHeader = "accept-encoding";

acceptHeader

const String acceptHeader
Implementation
dart
static const acceptHeader = "accept";

acceptLanguageHeader

const String acceptLanguageHeader
Implementation
dart
static const acceptLanguageHeader = "accept-language";

acceptRangesHeader

const String acceptRangesHeader
Implementation
dart
static const acceptRangesHeader = "accept-ranges";

accessControlAllowCredentialsHeader

const String accessControlAllowCredentialsHeader
Implementation
dart
@Since("2.14")
static const accessControlAllowCredentialsHeader =
    'access-control-allow-credentials';

accessControlAllowHeadersHeader

const String accessControlAllowHeadersHeader
Implementation
dart
@Since("2.14")
static const accessControlAllowHeadersHeader = 'access-control-allow-headers';

accessControlAllowMethodsHeader

const String accessControlAllowMethodsHeader
Implementation
dart
@Since("2.14")
static const accessControlAllowMethodsHeader = 'access-control-allow-methods';

accessControlAllowOriginHeader

const String accessControlAllowOriginHeader
Implementation
dart
@Since("2.14")
static const accessControlAllowOriginHeader = 'access-control-allow-origin';

accessControlExposeHeadersHeader

const String accessControlExposeHeadersHeader
Implementation
dart
@Since("2.14")
static const accessControlExposeHeadersHeader =
    'access-control-expose-headers';

accessControlMaxAgeHeader

const String accessControlMaxAgeHeader
Implementation
dart
@Since("2.14")
static const accessControlMaxAgeHeader = 'access-control-max-age';

accessControlRequestHeadersHeader

const String accessControlRequestHeadersHeader
Implementation
dart
@Since("2.14")
static const accessControlRequestHeadersHeader =
    'access-control-request-headers';

accessControlRequestMethodHeader

const String accessControlRequestMethodHeader
Implementation
dart
@Since("2.14")
static const accessControlRequestMethodHeader =
    'access-control-request-method';

ageHeader

const String ageHeader
Implementation
dart
static const ageHeader = "age";

allowHeader

const String allowHeader
Implementation
dart
static const allowHeader = "allow";

authorizationHeader

const String authorizationHeader
Implementation
dart
static const authorizationHeader = "authorization";

cacheControlHeader

const String cacheControlHeader
Implementation
dart
static const cacheControlHeader = "cache-control";

connectionHeader

const String connectionHeader
Implementation
dart
static const connectionHeader = "connection";

contentDisposition

const String contentDisposition
Implementation
dart
static const contentDisposition = "content-disposition";

contentEncodingHeader

const String contentEncodingHeader
Implementation
dart
static const contentEncodingHeader = "content-encoding";

contentLanguageHeader

const String contentLanguageHeader
Implementation
dart
static const contentLanguageHeader = "content-language";

contentLengthHeader

const String contentLengthHeader
Implementation
dart
static const contentLengthHeader = "content-length";

contentLocationHeader

const String contentLocationHeader
Implementation
dart
static const contentLocationHeader = "content-location";

contentMD5Header

const String contentMD5Header
Implementation
dart
static const contentMD5Header = "content-md5";

contentRangeHeader

const String contentRangeHeader
Implementation
dart
static const contentRangeHeader = "content-range";

contentTypeHeader

const String contentTypeHeader
Implementation
dart
static const contentTypeHeader = "content-type";

cookieHeader

const String cookieHeader
Implementation
dart
static const cookieHeader = "cookie";

dateHeader

const String dateHeader
Implementation
dart
static const dateHeader = "date";

entityHeaders

const List<String> entityHeaders
Implementation
dart
static const entityHeaders = [
  allowHeader,
  contentEncodingHeader,
  contentLanguageHeader,
  contentLengthHeader,
  contentLocationHeader,
  contentMD5Header,
  contentRangeHeader,
  contentTypeHeader,
  expiresHeader,
  lastModifiedHeader,
];

etagHeader

const String etagHeader
Implementation
dart
static const etagHeader = "etag";

expectHeader

const String expectHeader
Implementation
dart
static const expectHeader = "expect";

expiresHeader

const String expiresHeader
Implementation
dart
static const expiresHeader = "expires";

fromHeader

const String fromHeader
Implementation
dart
static const fromHeader = "from";

generalHeaders

const List<String> generalHeaders
Implementation
dart
static const generalHeaders = [
  cacheControlHeader,
  connectionHeader,
  dateHeader,
  pragmaHeader,
  trailerHeader,
  transferEncodingHeader,
  upgradeHeader,
  viaHeader,
  warningHeader,
];

hostHeader

const String hostHeader
Implementation
dart
static const hostHeader = "host";

ifMatchHeader

const String ifMatchHeader
Implementation
dart
static const ifMatchHeader = "if-match";

ifModifiedSinceHeader

const String ifModifiedSinceHeader
Implementation
dart
static const ifModifiedSinceHeader = "if-modified-since";

ifNoneMatchHeader

const String ifNoneMatchHeader
Implementation
dart
static const ifNoneMatchHeader = "if-none-match";

ifRangeHeader

const String ifRangeHeader
Implementation
dart
static const ifRangeHeader = "if-range";

ifUnmodifiedSinceHeader

const String ifUnmodifiedSinceHeader
Implementation
dart
static const ifUnmodifiedSinceHeader = "if-unmodified-since";

lastModifiedHeader

const String lastModifiedHeader
Implementation
dart
static const lastModifiedHeader = "last-modified";

locationHeader

const String locationHeader
Implementation
dart
static const locationHeader = "location";

maxForwardsHeader

const String maxForwardsHeader
Implementation
dart
static const maxForwardsHeader = "max-forwards";

pragmaHeader

const String pragmaHeader
Implementation
dart
static const pragmaHeader = "pragma";

proxyAuthenticateHeader

const String proxyAuthenticateHeader
Implementation
dart
static const proxyAuthenticateHeader = "proxy-authenticate";

proxyAuthorizationHeader

const String proxyAuthorizationHeader
Implementation
dart
static const proxyAuthorizationHeader = "proxy-authorization";

rangeHeader

const String rangeHeader
Implementation
dart
static const rangeHeader = "range";

refererHeader

const String refererHeader
Implementation
dart
static const refererHeader = "referer";

requestHeaders

const List<String> requestHeaders
Implementation
dart
static const requestHeaders = [
  acceptHeader,
  acceptCharsetHeader,
  acceptEncodingHeader,
  acceptLanguageHeader,
  authorizationHeader,
  expectHeader,
  fromHeader,
  hostHeader,
  ifMatchHeader,
  ifModifiedSinceHeader,
  ifNoneMatchHeader,
  ifRangeHeader,
  ifUnmodifiedSinceHeader,
  maxForwardsHeader,
  proxyAuthorizationHeader,
  rangeHeader,
  refererHeader,
  teHeader,
  userAgentHeader,
];

responseHeaders

const List<String> responseHeaders
Implementation
dart
static const responseHeaders = [
  acceptRangesHeader,
  ageHeader,
  etagHeader,
  locationHeader,
  proxyAuthenticateHeader,
  retryAfterHeader,
  serverHeader,
  varyHeader,
  wwwAuthenticateHeader,
  contentDisposition,
];

retryAfterHeader

const String retryAfterHeader
Implementation
dart
static const retryAfterHeader = "retry-after";

serverHeader

const String serverHeader
Implementation
dart
static const serverHeader = "server";

setCookieHeader

const String setCookieHeader
Implementation
dart
static const setCookieHeader = "set-cookie";

teHeader

const String teHeader
Implementation
dart
static const teHeader = "te";

trailerHeader

const String trailerHeader
Implementation
dart
static const trailerHeader = "trailer";

transferEncodingHeader

const String transferEncodingHeader
Implementation
dart
static const transferEncodingHeader = "transfer-encoding";

upgradeHeader

const String upgradeHeader
Implementation
dart
static const upgradeHeader = "upgrade";

userAgentHeader

const String userAgentHeader
Implementation
dart
static const userAgentHeader = "user-agent";

varyHeader

const String varyHeader
Implementation
dart
static const varyHeader = "vary";

viaHeader

const String viaHeader
Implementation
dart
static const viaHeader = "via";

warningHeader

const String warningHeader
Implementation
dart
static const warningHeader = "warning";

wwwAuthenticateHeader

const String wwwAuthenticateHeader
Implementation
dart
static const wwwAuthenticateHeader = "www-authenticate";