How to Bypass SSL Certificate Verification in flutter?

3,844

You need to configure your HttpService to work with Self-Signed SSL local servers. Like this:


import 'dart:io';
import 'dart:convert';
class HttpService {
  Future<dynamic> sendRequestToServer(dynamic model, String                         reqType, bool isTokenHeader, String token) async {
      HttpClient client = new HttpClient();
      client.badCertificateCallback =((X509Certificate cert, String  host, int port) => true);
      HttpClientRequest request = await         client.postUrl(Uri.parse("https://${serverConstants.serverUrl}$reqType"));
      request.headers.set('Content-Type', 'application/json');
      if(isTokenHeader){
         request.headers.set('Authorization', 'Bearer $token');
      }
     request.add(utf8.encode(jsonEncode(model)));
     HttpClientResponse result = await request.close();
     if(result.statusCode == 200) {
        return jsonDecode(await result.transform(utf8.decoder)
        .join());
     } else {
        return null;
     }
   }
}

Read more from here.

Share:
3,844
Haroon Ahmed
Author by

Haroon Ahmed

Updated on December 25, 2022

Comments

  • Haroon Ahmed
    Haroon Ahmed over 1 year

    How to Bypass SSL Certificate Verification in flutter? Error: Handshake Exception: Handshake error in client(OS Error:CERTIFICATE_VERIFY_FAILED:self signed certificate(handshake.cc:345)

  • Haroon Ahmed
    Haroon Ahmed over 3 years
    how can call this function?
  • Dani3le_
    Dani3le_ about 2 years
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review