buildRequestHeaders method

Map<String, String> buildRequestHeaders(
  1. HttpClient client,
  2. HttpMethod method,
  3. String url, {
  4. Map<String, String>? headers,
  5. Authorization? authorization,
  6. String? contentType,
  7. String? accept,
})

Helper to build the request headers.

Implementation

Map<String, String> buildRequestHeaders(
  HttpClient client,
  HttpMethod method,
  String url, {
  Map<String, String>? headers,
  Authorization? authorization,
  String? contentType,
  String? accept,
}) {
  var requestHeaders = client.buildRequestHeaders(url) ?? <String, String>{};

  if (contentType != null && method != HttpMethod.GET) {
    requestHeaders[HttpRequest._headerKeyContentType] = contentType;
  }

  if (accept != null) {
    requestHeaders['Accept'] = accept;
  }

  if (authorization != null && authorization.usesAuthorizationHeader) {
    var credential = authorization.resolvedCredential!;
    var authorizationHeaderLine = credential.buildAuthorizationHeaderLine();
    if (authorizationHeaderLine != null) {
      requestHeaders['Authorization'] = authorizationHeaderLine;
    }
  }

  if (headers != null) {
    requestHeaders.addAll(headers);
  }

  return requestHeaders;
}