in postman
in Body -> from-data i have key = image and value file of image and type of key is File
in Authorization i have Bearer Token and send token in header i have Content-Type = multipart/form-data; boundary=
and a url is 'url/id'
when i send an image i got 202 status and everything is ok
now i write some code in my flutter application
method for take image :
Future<void> takePictureAndSend() async {
if (_timer != null) {
return;
}
try {
XFile picture = await controller.takePicture();
bool status = await sendImageToServer(picture);
if (!status) {
await Future.delayed(Duration(seconds: 5));
XFile picture2 = await controller.takePicture();
await sendImageToServer(picture2);
}
} catch (e) {
setState(() {
_isDetecting = false;
_pictureCount++;
});
Fluttertoast.showToast(
msg: "try again",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
backgroundColor: MyColors.darkRed,
textColor: Colors.white,
fontSize: 16.0,
);
}
}
method for send image to the server
Future<bool> sendImageToServer(XFile picture) async {
setState(() {
imageLoader = true;
});
final SharedPreferences prefs = await SharedPreferences.getInstance();
String apiUrl = '${MyTexts.url}/api/v1/detection/episode/${data['id']}/';
try {
File fileImage = File(picture.path);
var request = http.MultipartRequest('POST', Uri.parse(apiUrl));
request.headers['Authorization'] = 'Bearer $_token';
request.headers['Content-Type'] = 'multipart/form-data';
var fileTemp = await http.MultipartFile.fromPath('image', File(picture.path).path);
request.files.add(fileTemp);
var response = await request.send();
if (response.statusCode == 202) {
final responseBytes = await response.stream.toBytes();
final imageBytes = responseBytes.buffer.asUint8List();
setState(() {
tryAgain = false;
showResult = true;
greenBorder = true;
resultImage = imageBytes;
imageLoader = false;
});
Fluttertoast.showToast(
msg: "nice",
toastLength: Toast.LENGTH_SHORT,
timeInSecForIosWeb: 1,
backgroundColor: MyColors.greenNavigation,
textColor: Colors.white,
fontSize: 16.0,
);
Future.delayed(Duration(seconds: 5), () async {
setState(() {
showResult = false;
});
if (data['next_episode'] != null) {
await loadData(MyTexts.url + data['next_episode']);
} else {
if (Navigator.canPop(context)) {
Navigator.pop(context);
}
}
});
return true;
}
else {
setState(() {
imageLoader = false;
});
_takePicture();
return false;
}
} catch (e) {
print('errorrrrrrrrrrrr $e');
setState(() {
tryAgain = true;
});
return false;
}
}
but in my code i gote status 500 when i send an image to the server
and as i know this line request.files.add(fileTemp); return empty {}
so how can i fix my code ?
