1

When I try to post a request I get

[VERBOSE-2:ui_dart_state.cc(177)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error: 
CERTIFICATE_VERIFY_FAILED: ok(handshake.cc:354))

my code (provided in the example)

 Future login() async {
final request = {
  "j_username": "user1",
  "j_password": "pass1",
};

 final response = await _dio.post('/itim/restlogin/login.jsp', data: request);
//get cooking from response
final cookies = response.headers.map['set-cookie'];
if (cookies.isNotEmpty && cookies.length == 2) {
  final authToken = cookies[1].split(';')[0]; //it depends on how your server sending cookie
  //save this authToken in local storage, and pass in further api calls.

  aToken = authToken;
  print("authToken");
//saving this to global variable to refresh current api calls to add cookie.
  print(authToken);
}

print(cookies);
//print(response.headers.toString());

}

Ken White
  • 123,280
  • 14
  • 225
  • 444
Farhan Fida
  • 172
  • 3
  • 12

1 Answers1

5

This means your website doesn't have a valid certificate.

Please add the below code to fix this.

 (_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
         (HttpClient dioClient) {
       dioClient.badCertificateCallback =
           ((X509Certificate cert, String host, int port) => true);
       return dioClient;
     };

Like this

Future login() async {
final request = {
  "j_username": "user1",
  "j_password": "pass1",
};
(_dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
             (HttpClient dioClient) {
           dioClient.badCertificateCallback =
               ((X509Certificate cert, String host, int port) => true);
           return dioClient;
         };

 final response = await _dio.post('/itim/restlogin/login.jsp', data: request);
//get cooking from response
final cookies = response.headers.map['set-cookie'];
if (cookies.isNotEmpty && cookies.length == 2) {
  final authToken = cookies[1].split(';')[0]; //it depends on how your server sending cookie
  //save this authToken in local storage, and pass in further api calls.

  aToken = authToken;
  print("authToken");
//saving this to global variable to refresh current api calls to add cookie.
  print(authToken);
}

print(cookies);

Make sure you comment code when you have a valid certificate.

Jigar Fumakiya
  • 2,039
  • 1
  • 8
  • 21