I’m developing my own API to receive files and using Flutter to create the app.
So I’m sending files to and API, using Flutter Dio, and I keep getting error 406.
'file': await MultipartFile.fromFile(file.path),
Turns out, I found out if I add the following line, it works:
'file_tar': await MultipartFile.fromFile(file.path, contentType: MediaType('application', 'x-tar')),
To work, both lines must be on the code.
On PHP web, the same thing, but I couldn’t find a workaround.
Funny thing, using the same API on Postman, and over there it doesn’t require this line, so I guess this might be Flutter Dio encoding the file somehow, requiring the x-tar Content-Type for the file.
Here is the entire Flutter code:
var dio = Dio();
var formData = FormData.fromMap({
'file': await MultipartFile.fromFile(file.path),
'file_tar': await MultipartFile.fromFile(file.path, contentType: MediaType('application', 'x-tar')),
});
var response = await dio.post(url,
data: formData,
options: Options(headers: {
'Content-Type': 'multipart/form-data; charset=utf-8',
'Accept': '*/*',
'Connection': 'keep-alive',
'Accept-Encoding': 'gzip, deflate, br',
}), onSendProgress: (int sent, int total) {
var progress = (sent / total) * 100;
log('Progress: $progress %');
});
Thanks!!!
2
Answers
You can try this way also. It might help you.
OR
file_tar is your key then its fine. otherwise you can just write files as in the end its creates array of files.
Might be content-type issue. Try this code: