Appearance
HttpHeaders abstract interface
abstract interface class HttpHeadersHeaders for HTTP requests and responses.
In some situations, headers are immutable:
HttpRequest and HttpClientResponse always have immutable headers.
HttpResponse and HttpClientRequest have immutable headers from the moment the body is written to.
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 chunkedTransferEncodinggetter:
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 contentLengthgetter:
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? contentTypegetter:
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? dategetter:
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? expiresgetter:
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 hashCodeThe 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? hostgetter:
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? ifModifiedSincegetter:
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 persistentConnectiongetter:
Whether the connection is persistent (keep-alive).
setter:
Whether the connection is persistent (keep-alive).
Implementation
dart
late bool persistentConnection;port read / write
int? portgetter:
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 runtimeTypeA representation of the runtime type of the object.
Inherited from Object.
Implementation
dart
external Type get runtimeType;Methods
add()
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()
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 errorThis 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()
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()
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()
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
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 == omust be true.Symmetric: For all objects
o1ando2,o1 == o2ando2 == o1must either both be true, or both be false.Transitive: For all objects
o1,o2, ando3, ifo1 == o2ando2 == o3are true, theno1 == o3must 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
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 acceptCharsetHeaderImplementation
dart
static const acceptCharsetHeader = "accept-charset";acceptEncodingHeader
const String acceptEncodingHeaderImplementation
dart
static const acceptEncodingHeader = "accept-encoding";acceptHeader
const String acceptHeaderImplementation
dart
static const acceptHeader = "accept";acceptLanguageHeader
const String acceptLanguageHeaderImplementation
dart
static const acceptLanguageHeader = "accept-language";acceptRangesHeader
const String acceptRangesHeaderImplementation
dart
static const acceptRangesHeader = "accept-ranges";accessControlAllowCredentialsHeader
const String accessControlAllowCredentialsHeaderImplementation
dart
@Since("2.14")
static const accessControlAllowCredentialsHeader =
'access-control-allow-credentials';accessControlAllowHeadersHeader
const String accessControlAllowHeadersHeaderImplementation
dart
@Since("2.14")
static const accessControlAllowHeadersHeader = 'access-control-allow-headers';accessControlAllowMethodsHeader
const String accessControlAllowMethodsHeaderImplementation
dart
@Since("2.14")
static const accessControlAllowMethodsHeader = 'access-control-allow-methods';accessControlAllowOriginHeader
const String accessControlAllowOriginHeaderImplementation
dart
@Since("2.14")
static const accessControlAllowOriginHeader = 'access-control-allow-origin';accessControlExposeHeadersHeader
const String accessControlExposeHeadersHeaderImplementation
dart
@Since("2.14")
static const accessControlExposeHeadersHeader =
'access-control-expose-headers';accessControlMaxAgeHeader
const String accessControlMaxAgeHeaderImplementation
dart
@Since("2.14")
static const accessControlMaxAgeHeader = 'access-control-max-age';accessControlRequestHeadersHeader
const String accessControlRequestHeadersHeaderImplementation
dart
@Since("2.14")
static const accessControlRequestHeadersHeader =
'access-control-request-headers';accessControlRequestMethodHeader
const String accessControlRequestMethodHeaderImplementation
dart
@Since("2.14")
static const accessControlRequestMethodHeader =
'access-control-request-method';ageHeader
const String ageHeaderImplementation
dart
static const ageHeader = "age";allowHeader
const String allowHeaderImplementation
dart
static const allowHeader = "allow";authorizationHeader
const String authorizationHeaderImplementation
dart
static const authorizationHeader = "authorization";cacheControlHeader
const String cacheControlHeaderImplementation
dart
static const cacheControlHeader = "cache-control";connectionHeader
const String connectionHeaderImplementation
dart
static const connectionHeader = "connection";contentDisposition
const String contentDispositionImplementation
dart
static const contentDisposition = "content-disposition";contentEncodingHeader
const String contentEncodingHeaderImplementation
dart
static const contentEncodingHeader = "content-encoding";contentLanguageHeader
const String contentLanguageHeaderImplementation
dart
static const contentLanguageHeader = "content-language";contentLengthHeader
const String contentLengthHeaderImplementation
dart
static const contentLengthHeader = "content-length";contentLocationHeader
const String contentLocationHeaderImplementation
dart
static const contentLocationHeader = "content-location";contentMD5Header
const String contentMD5HeaderImplementation
dart
static const contentMD5Header = "content-md5";contentRangeHeader
const String contentRangeHeaderImplementation
dart
static const contentRangeHeader = "content-range";contentTypeHeader
const String contentTypeHeaderImplementation
dart
static const contentTypeHeader = "content-type";cookieHeader
const String cookieHeaderImplementation
dart
static const cookieHeader = "cookie";dateHeader
const String dateHeaderImplementation
dart
static const dateHeader = "date";entityHeaders
Implementation
dart
static const entityHeaders = [
allowHeader,
contentEncodingHeader,
contentLanguageHeader,
contentLengthHeader,
contentLocationHeader,
contentMD5Header,
contentRangeHeader,
contentTypeHeader,
expiresHeader,
lastModifiedHeader,
];etagHeader
const String etagHeaderImplementation
dart
static const etagHeader = "etag";expectHeader
const String expectHeaderImplementation
dart
static const expectHeader = "expect";expiresHeader
const String expiresHeaderImplementation
dart
static const expiresHeader = "expires";fromHeader
const String fromHeaderImplementation
dart
static const fromHeader = "from";generalHeaders
Implementation
dart
static const generalHeaders = [
cacheControlHeader,
connectionHeader,
dateHeader,
pragmaHeader,
trailerHeader,
transferEncodingHeader,
upgradeHeader,
viaHeader,
warningHeader,
];hostHeader
const String hostHeaderImplementation
dart
static const hostHeader = "host";ifMatchHeader
const String ifMatchHeaderImplementation
dart
static const ifMatchHeader = "if-match";ifModifiedSinceHeader
const String ifModifiedSinceHeaderImplementation
dart
static const ifModifiedSinceHeader = "if-modified-since";ifNoneMatchHeader
const String ifNoneMatchHeaderImplementation
dart
static const ifNoneMatchHeader = "if-none-match";ifRangeHeader
const String ifRangeHeaderImplementation
dart
static const ifRangeHeader = "if-range";ifUnmodifiedSinceHeader
const String ifUnmodifiedSinceHeaderImplementation
dart
static const ifUnmodifiedSinceHeader = "if-unmodified-since";lastModifiedHeader
const String lastModifiedHeaderImplementation
dart
static const lastModifiedHeader = "last-modified";locationHeader
const String locationHeaderImplementation
dart
static const locationHeader = "location";maxForwardsHeader
const String maxForwardsHeaderImplementation
dart
static const maxForwardsHeader = "max-forwards";pragmaHeader
const String pragmaHeaderImplementation
dart
static const pragmaHeader = "pragma";proxyAuthenticateHeader
const String proxyAuthenticateHeaderImplementation
dart
static const proxyAuthenticateHeader = "proxy-authenticate";proxyAuthorizationHeader
const String proxyAuthorizationHeaderImplementation
dart
static const proxyAuthorizationHeader = "proxy-authorization";rangeHeader
const String rangeHeaderImplementation
dart
static const rangeHeader = "range";refererHeader
const String refererHeaderImplementation
dart
static const refererHeader = "referer";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
Implementation
dart
static const responseHeaders = [
acceptRangesHeader,
ageHeader,
etagHeader,
locationHeader,
proxyAuthenticateHeader,
retryAfterHeader,
serverHeader,
varyHeader,
wwwAuthenticateHeader,
contentDisposition,
];retryAfterHeader
const String retryAfterHeaderImplementation
dart
static const retryAfterHeader = "retry-after";serverHeader
const String serverHeaderImplementation
dart
static const serverHeader = "server";setCookieHeader
const String setCookieHeaderImplementation
dart
static const setCookieHeader = "set-cookie";teHeader
const String teHeaderImplementation
dart
static const teHeader = "te";trailerHeader
const String trailerHeaderImplementation
dart
static const trailerHeader = "trailer";transferEncodingHeader
const String transferEncodingHeaderImplementation
dart
static const transferEncodingHeader = "transfer-encoding";upgradeHeader
const String upgradeHeaderImplementation
dart
static const upgradeHeader = "upgrade";userAgentHeader
const String userAgentHeaderImplementation
dart
static const userAgentHeader = "user-agent";varyHeader
const String varyHeaderImplementation
dart
static const varyHeader = "vary";viaHeader
const String viaHeaderImplementation
dart
static const viaHeader = "via";warningHeader
const String warningHeaderImplementation
dart
static const warningHeader = "warning";wwwAuthenticateHeader
const String wwwAuthenticateHeaderImplementation
dart
static const wwwAuthenticateHeader = "www-authenticate";