headersToMap function

Map<String, String> headersToMap(
  1. HttpHeaders headers
)

Converts an HttpHeaders instance into a Map suitable for sending over platform channels or displaying in Chrome DevTools.

Only the first header value for each key is preserved. Headers are case-sensitive; an explicit 'Content-Type' key is duplicated to match Chrome DevTools expectations.

Implementation

Map<String, String> headersToMap(HttpHeaders headers) {
  final Map<String, String> map = {};

  headers.forEach((header, values) {
    // Take only the first value for each header key
    map[header] = values.first;

    // Add an explicit capitalized Content-Type header as Chrome expects
    if (header.toLowerCase() == 'content-type') {
      map['Content-Type'] = values.first;
    }
  });

  return map;
}