Headers class

Headers is a case-insensitive, unmodifiable map that stores headers.

Headers provide both raw access via map operations and type-safe accessors for standard HTTP headers.

Accessing Headers

router.get('/info', (req) {
  final headers = req.headers;

  // Type-safe accessors
  final userAgent = headers.userAgent;
  final contentType = headers.contentType;
  final contentLength = headers.contentLength;

  // Raw access
  final custom = headers['X-Custom-Header'];

  return Response.ok();
});

Setting Headers

final headers = Headers.build((h) {
  // Set standard headers
  h.contentType = ContentTypeHeader(
    mimeType: MimeType.json,
    charset: 'utf-8',
  );

  h.cacheControl = CacheControlHeader(
    maxAge: 3600,
    publicCache: true,
  );

  // Set custom headers
  h['X-API-Version'] = ['2.0'];
  h['X-Request-ID'] = ['abc123'];
});

Response.ok(headers: headers, body: Body.fromString('data'));

Common Type-Safe Headers

// Request headers
headers.authorization; // AuthorizationHeader?
headers.cookie;        // List<Cookie>
headers.accept;        // List<MediaType>
headers.userAgent;     // String?
headers.host;          // HostHeader?

// Response headers
headers.cacheControl;  // CacheControlHeader?
headers.setCookie;     // SetCookieHeader?
headers.location;      // Uri?
headers.contentType;   // ContentTypeHeader?
Inheritance
Available extensions

Constructors

Headers.build(void update(MutableHeaders))
factory
Headers.empty()
factory
Headers.fromMap(Map<String, Iterable<String>>? values)
factory

Properties

accept AcceptHeader?

Available on Headers, provided by the HeadersEx extension

no setter
acceptEncoding AcceptEncodingHeader?

Available on Headers, provided by the HeadersEx extension

no setter
acceptLanguage AcceptLanguageHeader?

Available on Headers, provided by the HeadersEx extension

no setter
acceptRanges AcceptRangesHeader?

Available on Headers, provided by the HeadersEx extension

no setter
accessControlAllowCredentials bool?

Available on Headers, provided by the HeadersEx extension

no setter
accessControlAllowHeaders AccessControlAllowHeadersHeader?

Available on Headers, provided by the HeadersEx extension

no setter
accessControlAllowMethods AccessControlAllowMethodsHeader?

Available on Headers, provided by the HeadersEx extension

no setter
accessControlAllowOrigin AccessControlAllowOriginHeader?

Available on Headers, provided by the HeadersEx extension

no setter
accessControlExposeHeaders AccessControlExposeHeadersHeader?

Available on Headers, provided by the HeadersEx extension

no setter
accessControlMaxAge int?

Available on Headers, provided by the HeadersEx extension

no setter
accessControlRequestHeaders List<String>?

Available on Headers, provided by the HeadersEx extension

no setter
accessControlRequestMethod Method?

Available on Headers, provided by the HeadersEx extension

no setter
age int?

Available on Headers, provided by the HeadersEx extension

no setter
allow Set<Method>?

Available on Headers, provided by the HeadersEx extension

no setter
authorization AuthorizationHeader?

Available on Headers, provided by the HeadersEx extension

no setter
cacheControl CacheControlHeader?

Available on Headers, provided by the HeadersEx extension

no setter
clearSiteData ClearSiteDataHeader?

Available on Headers, provided by the HeadersEx extension

no setter
connection ConnectionHeader?

Available on Headers, provided by the HeadersEx extension

no setter
contentDisposition ContentDispositionHeader?

Available on Headers, provided by the HeadersEx extension

no setter
contentEncoding ContentEncodingHeader?

Available on Headers, provided by the HeadersEx extension

no setter
contentLanguage ContentLanguageHeader?

Available on Headers, provided by the HeadersEx extension

no setter
contentLength int?

Available on Headers, provided by the HeadersEx extension

no setter
contentLocation Uri?

Available on Headers, provided by the HeadersEx extension

no setter
contentRange ContentRangeHeader?

Available on Headers, provided by the HeadersEx extension

no setter
contentSecurityPolicy ContentSecurityPolicyHeader?

Available on Headers, provided by the HeadersEx extension

no setter

Available on Headers, provided by the HeadersEx extension

no setter
crossOriginEmbedderPolicy CrossOriginEmbedderPolicyHeader?

Available on Headers, provided by the HeadersEx extension

no setter
crossOriginOpenerPolicy CrossOriginOpenerPolicyHeader?

Available on Headers, provided by the HeadersEx extension

no setter
crossOriginResourcePolicy CrossOriginResourcePolicyHeader?

Available on Headers, provided by the HeadersEx extension

no setter
date DateTime?

Available on Headers, provided by the HeadersEx extension

no setter
entries Iterable<MapEntry<String, Iterable<String>>>
The map entries of this Map.
no setterinherited
etag ETagHeader?

Available on Headers, provided by the HeadersEx extension

no setter
expect ExpectHeader?

Available on Headers, provided by the HeadersEx extension

no setter
expires DateTime?

Available on Headers, provided by the HeadersEx extension

no setter
forwarded ForwardedHeader?

Available on Headers, provided by the HeadersEx extension

no setter
from FromHeader?

Available on Headers, provided by the HeadersEx extension

no setter
hashCode int
The hash code for this object.
no setterinherited
host HostHeader?

Available on Headers, provided by the HeadersEx extension

no setter
ifMatch IfMatchHeader?

Available on Headers, provided by the HeadersEx extension

no setter
ifModifiedSince DateTime?

Available on Headers, provided by the HeadersEx extension

no setter
ifNoneMatch IfNoneMatchHeader?

Available on Headers, provided by the HeadersEx extension

no setter
ifRange IfRangeHeader?

Available on Headers, provided by the HeadersEx extension

no setter
ifUnmodifiedSince DateTime?

Available on Headers, provided by the HeadersEx extension

no setter
isEmpty bool
Whether there is no key/value pair in the map.
no setterinherited
isNotEmpty bool
Whether there is at least one key/value pair in the map.
no setterinherited
keys Iterable<String>
The keys of this Map.
no setterinherited
lastModified DateTime?

Available on Headers, provided by the HeadersEx extension

no setter
length int
The number of key/value pairs in the map.
no setterinherited
location Uri?

Available on Headers, provided by the HeadersEx extension

no setter
maxForwards int?

Available on Headers, provided by the HeadersEx extension

no setter
origin Uri?

Available on Headers, provided by the HeadersEx extension

no setter
permissionsPolicy PermissionsPolicyHeader?

Available on Headers, provided by the HeadersEx extension

no setter
proxyAuthenticate AuthenticationHeader?

Available on Headers, provided by the HeadersEx extension

no setter
proxyAuthorization AuthorizationHeader?

Available on Headers, provided by the HeadersEx extension

no setter
range RangeHeader?

Available on Headers, provided by the HeadersEx extension

no setter
referer Uri?

Available on Headers, provided by the HeadersEx extension

no setter
referrerPolicy ReferrerPolicyHeader?

Available on Headers, provided by the HeadersEx extension

no setter
retryAfter RetryAfterHeader?

Available on Headers, provided by the HeadersEx extension

no setter
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
secFetchDest SecFetchDestHeader?

Available on Headers, provided by the HeadersEx extension

no setter
secFetchMode SecFetchModeHeader?

Available on Headers, provided by the HeadersEx extension

no setter
secFetchSite SecFetchSiteHeader?

Available on Headers, provided by the HeadersEx extension

no setter
server String?

Available on Headers, provided by the HeadersEx extension

no setter
setCookie SetCookieHeader?

Available on Headers, provided by the HeadersEx extension

no setter
strictTransportSecurity StrictTransportSecurityHeader?

Available on Headers, provided by the HeadersEx extension

no setter
te TEHeader?

Available on Headers, provided by the HeadersEx extension

no setter
trailer List<String>?

Available on Headers, provided by the HeadersEx extension

no setter
transferEncoding TransferEncodingHeader?

Available on Headers, provided by the HeadersEx extension

no setter
upgrade UpgradeHeader?

Available on Headers, provided by the HeadersEx extension

no setter
userAgent String?

Available on Headers, provided by the HeadersEx extension

no setter
values Iterable<Iterable<String>>
The values of this Map.
no setterinherited
vary VaryHeader?

Available on Headers, provided by the HeadersEx extension

no setter
via List<String>?

Available on Headers, provided by the HeadersEx extension

no setter
wwwAuthenticate AuthenticationHeader?

Available on Headers, provided by the HeadersEx extension

no setter
xForwardedFor XForwardedForHeader?

Available on Headers, provided by the HeadersEx extension

no setter
xPoweredBy String?

Available on Headers, provided by the HeadersEx extension

no setter

Methods

addAll(Map<String, Iterable<String>> other) → void
This operation is not supported by an unmodifiable map.
inherited
addEntries(Iterable<MapEntry<String, Iterable<String>>> entries) → void
This operation is not supported by an unmodifiable map.
inherited
cast<RK, RV>() Map<RK, RV>
Provides a view of this map as having RK keys and RV instances, if necessary.
inherited
clear() → void
This operation is not supported by an unmodifiable map.
inherited
containsKey(Object? key) bool
Whether this map contains the given key.
inherited
containsValue(Object? value) bool
Whether this map contains the given value.
inherited
forEach(void action(String key, Iterable<String> value)) → void
Applies action to each key/value pair of the map.
inherited
map<K2, V2>(MapEntry<K2, V2> transform(String key, Iterable<String> value)) Map<K2, V2>
Returns a new map where all entries of this map are transformed by the given convert function.
inherited
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
putIfAbsent(String key, Iterable<String> ifAbsent()) Iterable<String>
This operation is not supported by an unmodifiable map.
inherited
remove(Object? key) Iterable<String>?
This operation is not supported by an unmodifiable map.
inherited
removeWhere(bool test(String key, Iterable<String> value)) → void
This operation is not supported by an unmodifiable map.
inherited
toString() String
A string representation of this object.
inherited
transform(void update(MutableHeaders)) Headers
update(String key, Iterable<String> update(Iterable<String> value), {Iterable<String> ifAbsent()?}) Iterable<String>
This operation is not supported by an unmodifiable map.
inherited
updateAll(Iterable<String> update(String key, Iterable<String> value)) → void
This operation is not supported by an unmodifiable map.
inherited

Operators

operator ==(Object other) bool
The equality operator.
inherited
operator [](Object? key) Iterable<String>?
The value for the given key, or null if key is not in the map.
inherited
operator []=(String key, Iterable<String> value) → void
This operation is not supported by an unmodifiable map.
inherited

Constants

accept → const HeaderAccessor<AcceptHeader>
Common Headers (Used in Both Requests and Responses)
acceptEncoding → const HeaderAccessor<AcceptEncodingHeader>
acceptEncodingHeader → const String
acceptHeader → const String
Request Headers
acceptLanguage → const HeaderAccessor<AcceptLanguageHeader>
acceptLanguageHeader → const String
acceptRanges → const HeaderAccessor<AcceptRangesHeader>
acceptRangesHeader → const String
Common Headers (Used in Both Requests and Responses)
accessControlAllowCredentials → const HeaderAccessor<bool>
accessControlAllowCredentialsHeader → const String
Response Headers
accessControlAllowHeaders → const HeaderAccessor<AccessControlAllowHeadersHeader>
accessControlAllowHeadersHeader → const String
accessControlAllowMethods → const HeaderAccessor<AccessControlAllowMethodsHeader>
accessControlAllowMethodsHeader → const String
accessControlAllowOrigin → const HeaderAccessor<AccessControlAllowOriginHeader>
accessControlAllowOriginHeader → const String
accessControlExposeHeaders → const HeaderAccessor<AccessControlExposeHeadersHeader>
accessControlExposeHeadersHeader → const String
accessControlMaxAge → const HeaderAccessor<int>
accessControlMaxAgeHeader → const String
accessControlRequestHeaders → const HeaderAccessor<List<String>>
accessControlRequestHeadersHeader → const String
accessControlRequestMethod → const HeaderAccessor<Method>
accessControlRequestMethodHeader → const String
age → const HeaderAccessor<int>
ageHeader → const String
all → const Set<HeaderAccessor<Object>>
allow → const HeaderAccessor<SplayTreeSet<Method>>
allowHeader → const String
authorization → const HeaderAccessor<AuthorizationHeader>
authorizationHeader → const String
cacheControl → const HeaderAccessor<CacheControlHeader>
cacheControlHeader → const String
clearSiteData → const HeaderAccessor<ClearSiteDataHeader>
clearSiteDataHeader → const String
connection → const HeaderAccessor<ConnectionHeader>
connectionHeader → const String
contentDisposition → const HeaderAccessor<ContentDispositionHeader>
contentDispositionHeader → const String
contentEncoding → const HeaderAccessor<ContentEncodingHeader>
contentEncodingHeader → const String
contentLanguage → const HeaderAccessor<ContentLanguageHeader>
contentLanguageHeader → const String
contentLength → const HeaderAccessor<int>
contentLengthHeader → const String
contentLocation → const HeaderAccessor<Uri>
contentLocationHeader → const String
contentRange → const HeaderAccessor<ContentRangeHeader>
contentRangeHeader → const String
contentSecurityPolicy → const HeaderAccessor<ContentSecurityPolicyHeader>
contentSecurityPolicyHeader → const String
contentTypeHeader → const String
cookieHeader → const String
crossOriginEmbedderPolicy → const HeaderAccessor<CrossOriginEmbedderPolicyHeader>
crossOriginEmbedderPolicyHeader → const String
crossOriginOpenerPolicy → const HeaderAccessor<CrossOriginOpenerPolicyHeader>
crossOriginOpenerPolicyHeader → const String
crossOriginResourcePolicy → const HeaderAccessor<CrossOriginResourcePolicyHeader>
crossOriginResourcePolicyHeader → const String
date → const HeaderAccessor<DateTime>
Date-related headers
dateHeader → const String
General Headers
etag → const HeaderAccessor<ETagHeader>
etagHeader → const String
expect → const HeaderAccessor<ExpectHeader>
expectHeader → const String
expires → const HeaderAccessor<DateTime>
expiresHeader → const String
forwarded → const HeaderAccessor<ForwardedHeader>
forwardedHeader → const String
from → const HeaderAccessor<FromHeader>
Request Headers
fromHeader → const String
host → const HeaderAccessor<HostHeader>
hostHeader → const String
ifMatch → const HeaderAccessor<IfMatchHeader>
ifMatchHeader → const String
ifModifiedSince → const HeaderAccessor<DateTime>
ifModifiedSinceHeader → const String
ifNoneMatch → const HeaderAccessor<IfNoneMatchHeader>
ifNoneMatchHeader → const String
ifRange → const HeaderAccessor<IfRangeHeader>
ifRangeHeader → const String
ifUnmodifiedSince → const HeaderAccessor<DateTime>
ifUnmodifiedSinceHeader → const String
lastModified → const HeaderAccessor<DateTime>
lastModifiedHeader → const String
location → const HeaderAccessor<Uri>
Response Headers
locationHeader → const String
maxForwards → const HeaderAccessor<int>
maxForwardsHeader → const String
origin → const HeaderAccessor<Uri>
General Headers
originHeader → const String
permissionsPolicy → const HeaderAccessor<PermissionsPolicyHeader>
permissionsPolicyHeader → const String
proxyAuthenticate → const HeaderAccessor<AuthenticationHeader>
proxyAuthenticateHeader → const String
proxyAuthorization → const HeaderAccessor<AuthorizationHeader>
proxyAuthorizationHeader → const String
range → const HeaderAccessor<RangeHeader>
rangeHeader → const String
referer → const HeaderAccessor<Uri>
refererHeader → const String
referrerPolicy → const HeaderAccessor<ReferrerPolicyHeader>
referrerPolicyHeader → const String
request → const Set<HeaderAccessor<Object>>
response → const Set<HeaderAccessor<Object>>
retryAfter → const HeaderAccessor<RetryAfterHeader>
retryAfterHeader → const String
secFetchDest → const HeaderAccessor<SecFetchDestHeader>
secFetchDestHeader → const String
secFetchMode → const HeaderAccessor<SecFetchModeHeader>
secFetchModeHeader → const String
secFetchSite → const HeaderAccessor<SecFetchSiteHeader>
secFetchSiteHeader → const String
server → const HeaderAccessor<String>
serverHeader → const String
setCookie → const HeaderAccessor<SetCookieHeader>
setCookieHeader → const String
strictTransportSecurity → const HeaderAccessor<StrictTransportSecurityHeader>
Security and Modern Headers
strictTransportSecurityHeader → const String
Security and Modern Headers
te → const HeaderAccessor<TEHeader>
teHeader → const String
trailer → const HeaderAccessor<List<String>>
trailerHeader → const String
transferEncoding → const HeaderAccessor<TransferEncodingHeader>
transferEncodingHeader → const String
upgrade → const HeaderAccessor<UpgradeHeader>
upgradeHeader → const String
userAgent → const HeaderAccessor<String>
userAgentHeader → const String
vary → const HeaderAccessor<VaryHeader>
varyHeader → const String
via → const HeaderAccessor<List<String>>
viaHeader → const String
wwwAuthenticate → const HeaderAccessor<AuthenticationHeader>
wwwAuthenticateHeader → const String
xForwardedFor → const HeaderAccessor<XForwardedForHeader>
xForwardedForHeader → const String
xPoweredBy → const HeaderAccessor<String>
xPoweredByHeader → const String