headerAdd method

void headerAdd(
  1. String name,
  2. String value
)

Adds a HTTP header

Adds a HTTP header with the name and String value to the HTTP response.

The name is case-insensitive. The name is considered the same, whether it is represented using uppercase or lowercase letters.

The value is case sensitive.

HTTP allows for multiple headers with the same name: the new header is added after any existing headers with the same name.

Do not use this method for adding/setting the content type. Use the contentType member instead.

Do not use this method for adding/setting cookies. Use the cookieAdd and cookieDelete methods. An exception will be raised if the name matches "set-cookie".

Implementation

void headerAdd(String name, String value) {
  ArgumentError.checkNotNull(name);
  ArgumentError.checkNotNull(value);

  if (name.isEmpty) {
    throw ArgumentError.value(name, 'name', 'Empty string');
  }

  if (_headersOutputted) {
    throw StateError('Header already outputted');
  }

  final canonicalName = _headerCanonicalName(name);

  if (canonicalName == _headerCanonicalName('content-type')) {
    throw ArgumentError.value(
        canonicalName, 'name', 'use contentType to set Content-Type');
  }
  if (canonicalName == _headerCanonicalName('set-cookie')) {
    throw ArgumentError.value(
        canonicalName, 'name', 'use cookieAdd to set a cookie');
  }

  final _values = _headers[canonicalName];
  if (_values == null) {
    _headers[canonicalName] = <String>[value]; // create new list
  } else {
    _values.add(value); // append to existing list d
  }
}