I am trying to send a https
request from flutter with following function:
Future<void> sendOTP(PhoneNumber phoneNumber) async { try { final String url = 'https://127.0.0.1:5432/api/send_otp'; final response = await _dio.post( url, data: {'number': 1234567890}, options: Options( headers: {'Content-Type': 'application/json', }, ), ); if (response.statusCode != 200) { throw Exception("Failed to send OTP: ${response.statusCode}"); } } on DioError catch (e) { throw Exception("Failed to send OTP: ${e.error.toString()}"); } }
To enable ssl certificate
I have added following code in my main method:
void main() async { WidgetsFlutterBinding.ensureInitialized(); ByteData data = await PlatformAssetBundle().load('asset/certificate/certificate.pem'); SecurityContext.defaultContext .setTrustedCertificatesBytes(data.buffer.asUint8List()); runApp(const MyApp());}
I have downloaded the certificate.pem
file from https://letsencrypt.org/certs/lets-encrypt-r3.pem.
When I am sending the request using thunderclient
or postman
with the certificate, it works but from the flutter app it throws following error:
Error: HandshakeException: Handshake error in client (OS Error: TLSV1_ALERT_DECODE_ERROR(tls_record.cc:594))
For server, I am using actix-web
with rustls
- if relevant. Please suggest how do I fix the same?