0

I am fairly new to flutter. I am looking for some guidance on how to use flutter’s HHTP library correctly.

My challenge is consuming a WSDL service to upload a picture. Here are two codes (flutter vs. Java) that perform the same function with the same WSDL. Java works!

I build my flutter code using this example: How to upload image in Flutter. How to upload image in Flutter?

But my flutter code below returns server error 500: See screen shots below for reference and clarity.

Future<bool> sentPhotoTransaction () async {
 // URL includes two parameter plus the image file stream.
 String cIPhotoPath = "/storage/emulated/0/Android/data/com.saleson24.saleson24/files/Pictures/scaled_IMG_20200414_161101.jpg";
 String urlString = "http://pro.test.com/ImgHandler.WCFHost/FileManagerService.svc/UploadFile?MobID=20A47616&Sig=b6e61d4e3ee38";
Io.File imageFile;
 imageFile = new Io.File(cIPhotoPath);
 // ***************** create multipart request for POST *****************
 var request = http.MultipartRequest("POST", Uri.parse(urlString));
 // ***************** create multipart using filepath, string or bytes *****************
 var picture = await http.MultipartFile.fromPath("stream", imageFile.path);
 // ***************** add multipart for the picture to request *****************
 request.files.add(picture);
 try {
   var response = await request.send();
   if (response.statusCode == 200) {
     print("Success");
     return true;
   } else {
     print("POST request did not worked");
     return false;
   }
 } catch(e) {
   print(e.toString());
   return false;
 }
}

enter image description here enter image description here

Here is a Java code example that worked with the same WSDL:

    java.net.URL url = new URL(urlString);
    HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
    urlConnection.setDoOutput(true);    //Allow Outputs
    urlConnection.setUseCaches(false);  //Don't use a cached Copy
    urlConnection.setRequestMethod("POST");
    Bitmap full_image = BitmapFactory.decodeFile(filepath);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    full_image.compress(Bitmap.CompressFormat.JPEG, IMAGE_QUALITY, stream); // Convert stream.
    byte[] byteArray = stream.toByteArray();
    DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());
    dos.write(byteArray);
    dos.flush();
    dos.close();                 // END POST

How do I get the flutter code above using the HTTP library above to work? Is the HHTP Library the correct library to consume WSDL?

Your guidance is appreciated.
Stay @ home. Be safe!

Val
  • 101
  • 1
  • 10

2 Answers2

1

In Dart, at the moment, you're using a multipart request, in Java you're sending a stream. I suggest to try to send a stream too. Try to do it by using the fantastic dio library.

There is an example in there for sending it:

// Binary data
List<int> postData = <int>[...];
await dio.post(
  url,
  data: Stream.fromIterable(postData.map((e) => [e])), //create a Stream<List<int>>
  options: Options(
    headers: {
      Headers.contentLengthHeader: postData.length, // set content-length
    },
  ),
);

Let me know if you need something more in the comments. Hope it works.

  • I presume the picture data goes into definition of List postData = [...]; How do I achieve this? – Val Apr 16 '20 at 00:53
  • 1
    I think something like this: List fileBytes = await file.readAsBytes(); – Val Apr 16 '20 at 04:53
  • You're missing the code to compress the image, presumably to a lower resolution. – Richard Heap Apr 16 '20 at 09:24
  • The problem is on the request and not on the image. – Lorenzo Imperatrice Apr 16 '20 at 09:55
  • Hi Lorenzo, Thank you for the help and quick reply. In concept your answer above is good. But like Richard mentioned the code was incomplete. But I got it to work. I will post a full code sample shortly. Do did gave me the idea to research DIO library. I also got it to work using the http.MultipartRequest library as well. I am trying to figure which is best and faster the HTTP vs. the DIO library. – Val Apr 16 '20 at 22:09
0

This code expands on Lorenzo answer above. This code worked for me, hope that helps others.

    String photoPath = "";      // Your photo location path.
    Io.File file = new Io.File(photoPath);
    var dio = Dio();
    // ***************** Transfer File *****************
    try {
// Convert file to Bytes WITHOUT compression.
//          List<int> postData = await file.readAsBytes();                      
// Convert file to Bytes WITH compression.
          List<int> postData = await compressImageFileAndReturnList(file);      
      var response = await dio.post(urlString,
          data: Stream.fromIterable(postData.map((e) => [e])),
          options: Options(
              followRedirects: false,
              headers: {
                Headers.contentLengthHeader: postData.length, // set content-length
              }
          )
      );
      if (response.statusCode == 200) {
        print("Success");
        return true;
      } else {
        print("POST request did not worked");
        return false;
      }
    } catch(e) {
      print(e.toString());
      return false;
    }
Christopher Moore
  • 15,626
  • 10
  • 42
  • 52
Val
  • 101
  • 1
  • 10