I want to understand and know how to make a multiPart request with an mp4 file attached. While I understand there are posts that speak of dependencies such as dio or flutter_upload to do this, I don't want to cheat myself in knowing the why of things. So how do I accomplish this task? I keep getting the following error when making a request:
Error Snippet:
flutter: Failed due to: Unsupported Media Type and status code: 415
My endpoint only accepts Content-Types of application/JSON, but my app still threw when I tried to refactor my request to send json. I understand Dart requires data to be encoded before making a request but I am confused about how I do this with a MultipartFile
instance when I get the following error: The argument type 'String' can't be assigned to the parameter type 'MultipartFile'
. Finally, I refactored my function to reflect my latest attempt to send the correct format (despite no encoding involved :D).
Code Snippet:
Future<void> createAudioUpload(String fileUrl) async {
Uri ourUri = Uri.parse(
'https://[redacted]');
final request =
http.MultipartRequest('POST', ourUri); // creates our req instance
final file = await http.MultipartFile.fromPath('audio', fileUrl,
contentType: MediaType('audio', 'mp4')); // points a ref to file
request.files.add(file); // adds our file to our request instance
final response = await request.send(); // sends our request to the endpoint!
if (response.statusCode == 200) {
print('Success, $response');
} else {
print('Failed due to:${response.reasonPhrase}');
}
}
Code Snippet V2: (sending data as bytes)
Future<void> createAudioUpload(String fileUrl) async {
Uri ourUri = Uri.parse(
'[redacted]');
final request =
http.MultipartRequest('POST', ourUri); // creates our req instance
File ourFile = File(fileUrl);
List<int> fileBytes = ourFile.readAsBytesSync();
// String base64File = base64Encode(fileBytes);
final file = http.MultipartFile.fromBytes('flutter_sound', fileBytes,
filename: fileUrl.split("/").last,
);
request.files.add(file); // adds our file to our request instance
final response = await request.send(); // sends our request to the endpoint!
if (response.statusCode == 200) {
print('Success, $response');
} else {
print('Failed due to: ${response.reasonPhrase} and status code: ${response.statusCode} ');
}
}
Questions I want to understand (after I referenced flutter docs):
- What is supposed to be the first positional argument of
MultipartFile.fromPath
- What is the correct syntax to pass for application/json? would the arguments be passed as
"application", "json"
for MIME main and sub type per the docs at https://pub.dev/documentation/http_parser/latest/http_parser/MediaType/MediaType.html ?
Quick Note
- The data passes through an API gateway and is eventually stored in an S3 bucket.
- I referenced https://pub.dev/documentation/http/latest/http/MultipartRequest-class.html for constructing the code.
- If there is known documentation how to convert the audio file to bytes before sending that is also great!
Any help is appreciated.