Uploading File with Dio on Flutter without FormData

Viewed 83

I am trying to upload a file to the Dropbox API from a Flutter app. I am using Dio.

Here is my request:

try {
  FormData data = FormData.fromMap({
    "file": await MultipartFile.fromFile(
      filePath,
      filename: 'MyFile.db',
    ),
  });

  final dio = Dio();
  dio.options.headers['Authorization'] = 'Bearer $dropboxAccessToken';
  dio.options.headers['Content-Type'] = 'application/octet-stream';
  dio.options.headers['Dropbox-API-Arg'] =
      '{ "mode": "overwrite", "mute": false, "path": "/MyFile.db" }';

  final response = await dio
      .post('https://content.dropboxapi.com/2/files/upload', data: data);
  print('Success!');
  print(response);
} on DioError catch (error) {
  print(error.response);
}

It seems that Dio adds an additional boundary header when using FormData because this is the response I get:

Error in call to API function "files/upload": 
Bad HTTP "Content-Type" header: "multipart/form-data; boundary=--dio-boundary-0043340702"

The Content-Type header has to be application/octet-stream.

How can I upload a file in Dio without FormData? Or alternatively, how I can use FormData with a application/octet-stream content type and not inherit the custom header that Dio's FormData adds?

0 Answers
Related