I am trying to upload image using Flutter's http plugin.
Following is my code
Future<ImageUploadModel> postImage(File photoPath) async {
ImageUploadModel imageUploadModel;
var request = http.MultipartRequest(
"POST", Uri.parse("my_api_url"));
request.files.add(http.MultipartFile.fromBytes(
"photo", await File.fromUri(Uri.parse(photoPath.path)).readAsBytes(),
contentType: MediaType('image', 'jpg')));
final response = await request.send();
Uint8List responseByteArray = await response.stream.toBytes();
return standardSerializers.deserializeWith(ImageUploadModel.serializer, json.decode(utf8.decode(responseByteArray)));
}
I tried uploading image using Postman and the image get uploaded properly.
I have followed the links on stack but I am not able to resolve the error
Flutter: http post upload an image Flutter how to send multiple files to http post How to send an image to an api in dart/flutter? How to upload image in Flutter?
I am getting a status code of 200 but when I tried to access any variable from the response I get empty string.
I tried with Native Android using Kotlin and Retrofit and it works properly
I also tried the Dio
library
Dio dio = Dio();
FormData formdata = FormData();
formdata.add("photo", UploadFileInfo(photoPath, basename(photoPath.path)));
dio
.post("My_API_URL",
data: formdata,
options: Options(
method: 'POST',
responseType: ResponseType.json // or ResponseType.JSON
))
.then((response) {
print("2 $response");
return standardSerializers.deserializeWith(
ImageUploadModel.serializer, json.decode(response.data));
}).catchError((error) => print("3 $error"));
I don't want to convert my image to base64