0

I tried to upload a file to s3 using MultipartRequest in flutter but upon reach "response.send()" i get

I/flutter ( 8307): HandshakeException: Handshake error in client (OS Error:
I/flutter ( 8307):  CERTIFICATE_VERIFY_FAILED: self signed certificate in certificate chain(handshake.cc:354))

I turned 'SSL Certificate Verification' off in Postman for it work, so is there any way to turn it off in flutter whilst uploading?

here is the code i used:

var request = http.MultipartRequest('POST', uri)
    ..fields['key'] = data.key
    ..fields['x-amz-algorithm'] = data.algorithm
    ..fields['x-amz-credential'] = data.credential
    ..fields['x-amz-date'] = data.date
    ..fields['x-amz-security-token'] = data.securityToken
    ..fields['policy'] = data.policy
    ..files.add(await http.MultipartFile.fromPath('File', imagePath, filename: imageName));
    print(request.toString());
    try {
        var  response = await request.send();
    await for (var value in response.stream.transform(utf8.decoder)) {
        print(value);
     }
    } catch (e) {
    print(e.toString());
    }
Abhishek S
  • 21
  • 3

1 Answers1

1

If anyone faces the same, I was able to fix the above issue using "Dio" package like so

Dio _client = Dio();
_client.interceptors.add(LogInterceptor());
FormData formData = FormData.fromMap({
  'key': data.key,
  'x-amz-algorithm': data.algorithm,
  'x-amz-credential': data.credential,
  'x-amz-date': data.date,
  'x-amz-security-token': data.securityToken,
  'policy': data.policy,
  'x-amz-signature': data.signature,
  'File': await MultipartFile.fromFile(
    filePath,
    filename: fileName,
  )
});
(_client.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
    (HttpClient dioClient) {
  dioClient.badCertificateCallback =
      (X509Certificate cert, String host, int port) => true;
  return dio;
};
try {
  await _client.post(
    data.uploadUrl,
    data: formData,
  );
  _client.close();
} catch (e) {
  print(e.toString());
  _client.close();
}
Abhishek S
  • 21
  • 3