Flutter web http request badCertificateCallback

2,855

Make a http Client like this,

import 'dart:convert';
import 'dart:io';
import 'package:dio/adapter.dart';
import 'package:dio/dio.dart';
import 'package:flutter/foundation.dart';
import 'dart:io' as IO;
....
....
....
/// CLIENT
static Future<Dio> _dioClient() async {

Dio dio = Dio(await _getOptions()); // Getting Headers and Other data

if(!kIsWeb){
  (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
      (IO.HttpClient client) {
    client.badCertificateCallback =
        (X509Certificate cert, String host, int port) => true;
    return client;
  };
}
return dio;                                                             
}
Share:
2,855
Daniel Hernandez
Author by

Daniel Hernandez

Updated on December 19, 2022

Comments

  • Daniel Hernandez
    Daniel Hernandez over 1 year

    I was wondering if any you can point me to a web flutter library that had http badCertificateCallback. I tried DIO but it is giving me an error and submit an issue but I haven't heard from them yet

    DIO code:

    Dio dio = new Dio(options);
    (dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate =
    (client) {
    client.badCertificateCallback =
    (X509Certificate cert, String host, int port) => true;
    };
    
    Error: Expected a value of type 'DefaultHttpClientAdapter', but got one of type 'BrowserHttpClientAdapter'
    

    I also tried http, but it doesn't have a bad Certificate Callback, we could use this but it isn't web-compatible

    HttpClient httpClient = new HttpClient();
        httpClient.badCertificateCallback =
            ((X509Certificate cert, String host, int port) => true);
        IOClient ioClient = new IOClient(httpClient);
    response = await ioClient.post(url, body: data, headers: headers);
    

    Any comment will be more that apreciate.

    Thanks in advance, Daniel