header method

DoxResponse header(
  1. String key,
  2. dynamic value, {
  3. bool? replace,
})

set a header. It replaces the header with the same name, in any case (cache-control and Cache-Control are one header).

res.header('Authorization', 'Bearer xxx');

A value that is already on the response (for example one set by a middleware) stays, and this one is added as a second value. replace true removes that value first. When replace is null, CoreFlags.replaceHeaders decides, but Vary and Set-Cookie are lists, so the flag does not replace them (CORS adds Vary: Origin).

Implementation

DoxResponse header(String key, dynamic value, {bool? replace}) {
  Map<String, dynamic> map = headers;
  if (map.isNotEmpty) {
    String name = key.toLowerCase();
    map.removeWhere((String k, dynamic _) => k.toLowerCase() == name);
    _replaced?.removeWhere((String k) => k.toLowerCase() == name);
  }
  map[key] = value;
  if (replace ?? (Dox().flags.replaceHeaders && !_isList(key))) {
    (_replaced ??= <String>{}).add(key);
  }
  return this;
}