0

I want to accept the self-signed certificates which are not trusted. I'm unable to find a way to do this using http-package.

Can someone please help me modify below code to accept a self-signed certificate

import 'package:http/http.dart' show Client, Response;
import 'dart:convert';

  final Client _client = Client();

  Future<dynamic> postRequest(User user, String uri, String postBody,
      {decode: true}) async {
    try {
      var url = user.protocol + '://' + user.hostname + ':' + user.port + uri;
      String basicAuth =
          'Basic ' + base64Encode(utf8.encode('${user.username}:${user.password}'));
      var appHeaders = {
        'authorization': basicAuth,
        "Content-Type": "application/xml",
        "Accept": "application/json"
      };
      String resBody = await _client
          .post(url, body: postBody, headers: appHeaders)
          .then((Response res) => res.body);

      if (decode) {
        return json.decode(resBody);
      } else {
        return resBody;
      }
    } catch (e) {
      return e;
    }
  }
Vijay
  • 141
  • 1
  • 5
  • Possible duplicate of [How to do SSL pinning via self generated signed certificates in flutter?](https://stackoverflow.com/questions/51323603/how-to-do-ssl-pinning-via-self-generated-signed-certificates-in-flutter) – Richard Heap Aug 05 '19 at 10:56
  • It's not an exact duplicate, but the same solution will work - just return `true` from the closure. (Obviously, don't just return `true` in production. You could use the solution of the other question to pin your server certificate.) – Richard Heap Aug 05 '19 at 10:59

0 Answers0