multipartUpload method

Future<StreamedResponse?> multipartUpload({
  1. Map<String, String>? headers,
  2. Map<String, String>? body,
  3. required Map<String, List<String>> files,
  4. required String url,
  5. required String method,
})

CRUD over List

headers is a map of HTTP headers to send with the request

body is a map of key-value pairs to include in the request body

files is a map of pathName and List of paths to include in the request body

url is the URL to send request

method is the HTTP method to use (e.g. GET, POST, PUT, DELETE)

Example usage:

await RhUtils.instance.multipartUpload(
  files: {'avatar': ['/path/to/avatar.jpg', '/path/to/avatar.png']},
  url: 'https://example.com/api/users',
  method: HttpMethod.post,
);

Implementation

Future<http.StreamedResponse?> multipartUpload({
  Map<String, String>? headers,
  Map<String, String>? body,
  required Map<String, List<String>> files,
  required String url,
  required String method,
}) async {
  http.StreamedResponse? response;

  var request = http.MultipartRequest(
    method,
    Uri.parse(url),
  );
  if (headers != null) {
    request.headers.addAll(headers);
  }
  if (body != null) {
    request.fields.addAll(body);
  }
  if (files.isNotEmpty) {
    files.forEach((pathName, paths) async {
      for (var path in paths) {
        final multipart = await http.MultipartFile.fromPath(
          pathName,
          path,
        );
        request.files.add(multipart);
      }
    });
    response = await request.send();
  }

  return response;
}