applyHeaders function

Request applyHeaders (
  1. Request request,
  2. Map<String, String> headers,
  3. {bool override: true}
)

Apply given headers to the request if override is true, it will erase already present headers with the new value

final newRequest = applyHeaders(request, {'foo': 'bar'});

Implementation

Request applyHeaders(
  Request request,
  Map<String, String> headers, {
  bool override = true,
}) {
  final h = Map<String, String>.from(request.headers);

  for (var k in headers.keys) {
    if (!override && h.containsKey(k)) continue;
    h[k] = headers[k];
  }

  return request.replace(headers: h);
}