Why is this MultipartRequest working with curl, but not with Dart http?

33 Views Asked by At

Hey I am writing a Dart class that is connecting to a Seafile(cloud service) API and I want to upload files.

I read the docs and what I need to implement in Dart is equivalent to this curl command:

curl -H "Authorization: Token $TOKEN" \
    -F file=@$FILE \
    -F parent_dir=/ \
    $API_URL

I use the http package for my function:

Future<String> uploadFile(String fileUploadLink, XFile file, {String uploadDirectory='/'}) async {
    final url = Uri.parse(fileUploadLink); 
            
    var request = http.MultipartRequest('POST', url)
                ..headers['Authorization'] = 'Token $authToken'
                ..files.add(await http.MultipartFile.fromPath('file', file.path))
                ..fields['parent_dir'] = uploadDirectory; 

    var response = await request.send(); 
    return await response.stream.bytesToString();
}

When I call this function with api.uploadFile(link, XFile(filepath)) (with the same api link, filepath, and token) I receive 400: Bad Request.

What is my function missing that curl does?

EDIT:

I added debugging output to both curl(with --trace-ascii) and my function(with print(utf8.decode(await request.finalize().toBytes()));) and what I get is

curl:

00dd: Content-Type: multipart/form-data; boundary=--------------------
011d: ----ad99a2593a103a56
0133: 
0000: --------------------------ad99a2593a103a56
002c: Content-Disposition: form-data; name="file"; filename="test.rs"
006d: Content-Type: application/octet-stream
0095: 
0097: fn .
009d: --------------------------ad99a2593a103a56
00c9: Content-Disposition: form-data; name="parent_dir"
00fc: 
00fe: /
0101: --------------------------ad99a2593a103a56--

and dart:

--dart-http-boundary-c8fJRu6CZeiXuHTe2kWPn3v8jvTA5Ero683aN.yRBwUb8Mzgr0y
content-disposition: form-data; name="parent_dir"

/
--dart-http-boundary-c8fJRu6CZeiXuHTe2kWPn3v8jvTA5Ero683aN.yRBwUb8Mzgr0y
content-type: application/octet-stream
content-disposition: form-data; name="file"; filename="test.rs"

fn

--dart-http-boundary-c8fJRu6CZeiXuHTe2kWPn3v8jvTA5Ero683aN.yRBwUb8Mzgr0y--

So from the looks of it, it is actually sending the same contents.

0

There are 0 best solutions below