prepareRequest method

BaseRequest prepareRequest(
  1. HttpMethod method, {
  2. required Uri uri,
  3. required Map<String, String> headers,
  4. required Map<String, dynamic> params,
})
inherited

Implementation

http.BaseRequest prepareRequest(
  HttpMethod method, {
  required Uri uri,
  required Map<String, String> headers,
  required Map<String, dynamic> params,
}) {
  if (params.isNotEmpty) {
    params.removeWhere((key, value) => value == null);
  }

  http.BaseRequest request = http.Request(method.name(), uri);
  if (method == HttpMethod.get) {
    final encoded = <String, dynamic>{};
    if (params.isNotEmpty) {
      params.keys.forEach((key) {
        if (params[key] is int || params[key] is double) {
          encoded[key] = params[key].toString();
        } else if (params[key] is List) {
          encoded[key + "[]"] = params[key];
        } else {
          encoded[key] = params[key];
        }
      });
    }
    uri = Uri(
        fragment: uri.fragment,
        path: uri.path,
        host: uri.host,
        scheme: uri.scheme,
        queryParameters: encoded,
        port: uri.port);
    request = http.Request(method.name(), uri);
  } else {
    (request as http.Request).body = jsonEncode(params);
  }

  request.headers.addAll(headers);
  return request;
}