Headers 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:
request.headers.set(HttpHeaders.CACHE_CONTROL,
'max-age=3600, must-revalidate');
To retrieve the value of a header use the value()
method:
print(request.headers.value(HttpHeaders.USER_AGENT));
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.
Constants
- dynamic ACCEPT = "accept"
-
const
- dynamic ACCEPT_CHARSET = "accept-charset"
-
const
- dynamic ACCEPT_ENCODING = "accept-encoding"
-
const
- dynamic ACCEPT_LANGUAGE = "accept-language"
-
const
- dynamic ACCEPT_RANGES = "accept-ranges"
-
const
- dynamic AGE = "age"
-
const
- dynamic ALLOW = "allow"
-
const
- dynamic AUTHORIZATION = "authorization"
-
const
- dynamic CACHE_CONTROL = "cache-control"
-
const
- dynamic CONNECTION = "connection"
-
const
- dynamic CONTENT_ENCODING = "content-encoding"
-
const
- dynamic CONTENT_LANGUAGE = "content-language"
-
const
- dynamic CONTENT_LENGTH = "content-length"
-
const
- dynamic CONTENT_LOCATION = "content-location"
-
const
- dynamic CONTENT_MD5 = "content-md5"
-
const
- dynamic CONTENT_RANGE = "content-range"
-
const
- dynamic CONTENT_TYPE = "content-type"
-
const
- dynamic COOKIE = "cookie"
-
const
- dynamic DATE = "date"
-
const
- dynamic ENTITY_HEADERS = const [ALLOW, CONTENT_ENCODING, CONTENT_LANGUAGE, CONTENT_LENGTH, CONTENT_LOCATION, CONTENT_MD5, CONTENT_RANGE, CONTENT_TYPE, EXPIRES, LAST_MODIFIED]
-
const
- dynamic ETAG = "etag"
-
const
- dynamic EXPECT = "expect"
-
const
- dynamic EXPIRES = "expires"
-
const
- dynamic FROM = "from"
-
const
- dynamic GENERAL_HEADERS = const [CACHE_CONTROL, CONNECTION, DATE, PRAGMA, TRAILER, TRANSFER_ENCODING, UPGRADE, VIA, WARNING]
-
const
- dynamic HOST = "host"
-
const
- dynamic IF_MATCH = "if-match"
-
const
- dynamic IF_MODIFIED_SINCE = "if-modified-since"
-
const
- dynamic IF_NONE_MATCH = "if-none-match"
-
const
- dynamic IF_RANGE = "if-range"
-
const
- dynamic IF_UNMODIFIED_SINCE = "if-unmodified-since"
-
const
- dynamic LAST_MODIFIED = "last-modified"
-
const
- dynamic LOCATION = "location"
-
const
- dynamic MAX_FORWARDS = "max-forwards"
-
const
- dynamic PRAGMA = "pragma"
-
const
- dynamic PROXY_AUTHENTICATE = "proxy-authenticate"
-
const
- dynamic PROXY_AUTHORIZATION = "proxy-authorization"
-
const
- dynamic RANGE = "range"
-
const
- dynamic REFERER = "referer"
-
const
- dynamic REQUEST_HEADERS = const [ACCEPT, ACCEPT_CHARSET, ACCEPT_ENCODING, ACCEPT_LANGUAGE, AUTHORIZATION, EXPECT, FROM, HOST, IF_MATCH, IF_MODIFIED_SINCE, IF_NONE_MATCH, IF_RANGE, IF_UNMODIFIED_SINCE, MAX_FORWARDS, PROXY_AUTHORIZATION, RANGE, REFERER, TE, USER_AGENT]
-
const
- dynamic RESPONSE_HEADERS = const [ACCEPT_RANGES, AGE, ETAG, LOCATION, PROXY_AUTHENTICATE, RETRY_AFTER, SERVER, VARY, WWW_AUTHENTICATE]
-
const
- dynamic RETRY_AFTER = "retry-after"
-
const
- dynamic SERVER = "server"
-
const
- dynamic SET_COOKIE = "set-cookie"
-
const
- dynamic TE = "te"
-
const
- dynamic TRAILER = "trailer"
-
const
- dynamic TRANSFER_ENCODING = "transfer-encoding"
-
const
- dynamic UPGRADE = "upgrade"
-
const
- dynamic USER_AGENT = "user-agent"
-
const
- dynamic VARY = "vary"
-
const
- dynamic VIA = "via"
-
const
- dynamic WARNING = "warning"
-
const
- dynamic WWW_AUTHENTICATE = "www-authenticate"
-
const
Properties
- chunkedTransferEncoding → bool
-
read / write
Gets and sets the chunked transfer encoding header value.
- contentLength → int
-
read / write
Gets and sets the content length header value.
- contentType → ContentType
-
read / write
Gets and sets the content type. Note that the content type in the header will only be updated if this field is set directly. Mutating the returned current value will have no effect.
- date → DateTime
-
read / write
Gets and sets the date. The value of this property will reflect the 'date' header.
- expires → DateTime
-
read / write
Gets and sets the expiry date. The value of this property will reflect the 'expires' header.
- host → String
-
read / write
Gets and sets the host part of the 'host' header for the connection.
- ifModifiedSince → DateTime
-
read / write
Gets and sets the "if-modified-since" date. The value of this property will reflect the "if-modified-since" header.
- persistentConnection → bool
-
read / write
Gets and sets the persistent connection header value.
- port → int
-
read / write
Gets and sets the port part of the 'host' header for the connection.
Constructors
Operators
-
operator [](
String name) → List<String> -
Returns the list of values for the header named
name
. If there is no header with the provided name,null
will be returned.
Methods
-
add(
String name, Object value) → void -
Adds a header value. The header named
name
will have the valuevalue
added to its list of values. Some headers are single valued, and for these adding a value will replace the previous value. If the value is of type DateTime a HTTP date format will be applied. If the value is aList
each element of the list will be added separately. For all other types the defaulttoString
method will be used. -
clear(
) → void -
Remove all headers. Some headers have system supplied values and for these the system supplied values will still be added to the collection of values for the header.
-
forEach(
void f(String name, List<String> values)) → void -
Enumerates the headers, applying the function
f
to each header. The header name passed inname
will be all lower case. -
noFolding(
String name) → void -
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. -
remove(
String name, Object value) → void -
Removes a specific value for a header name. Some headers have system supplied values and for these the system supplied values will still be added to the collection of values for the header.
-
removeAll(
String name) → void -
Removes all values for the specified header name. Some headers have system supplied values and for these the system supplied values will still be added to the collection of values for the header.
-
set(
String name, Object value) → void -
Sets a header. The header named
name
will have all its values cleared before the valuevalue
is added as its value. -
value(
String name) → String -
Convenience method for the value for a single valued header. If there is no header with the provided name,
null
will be returned. If the header has more than one value an exception is thrown.