I have a json object and a multipartfile that a want to send to a server using multipartrequest in flutter,the multipartfile is good to go but the issue is that the request fields only accept string and I want to send a json object.
request.fields["objectField"] = entrepriseAgricultureCharegement.toJson(); // this line does not work because the field cant take a json object.
request.files.add(multipartFile);
here is the full function :
Future syncLocalDataToMobileSig(
{required EntrepriseAgricultureCharegement
entrepriseAgricultureCharegement}) async {
Uri uri = Uri.parse("$baseUrlMobile/new");
var request = http.MultipartRequest("POST", uri);
request.headers.addAll({"Content-Type": "multipart/form-data"});
File file = File(
"/data/user/0/com.example.app_amee/app_flutter/IMG_20220115_113910.jpg");
var stream = http.ByteStream(file.openRead());
stream.cast();
var length = await file.length();
var multipartFile = http.MultipartFile(
"files",
stream,
length,
);
request.fields["objectField"] = entrepriseAgricultureCharegement.toJson();
request.files.add(multipartFile);
try {
var streamedResponse = await request.send();
final responseData = await streamedResponse.stream.toBytes();
final responseString = String.fromCharCodes(responseData);
print(responseString);
} catch (e) {
print("e => $e");
}
}
does anyone have the solution to send the json object using multipartrequest