Petro
Petro

Reputation: 3662

Flutter How To Change http.dart Connection To SSL HTTPS

I'm currently using the http lib for a normal http connection. Does anyone know how to implement a certificate check into the http call so I can use SSL? I can't seem to find a clear answer on how to do this.


Here is a sample connection in my app:

  import 'package:http/http.dart' as http;


  String url = "https://www.mywebsite.com";
  print("Firing off url: ${url}");
  var request = http.MultipartRequest("POST", Uri.parse("${url}"));
  //cert check here maybe?



  //add POST fields
  request.fields["lookup"] = "true";
  request.fields["email"] = "[email protected]";
  request.fields["user_id"] = "1337";

  var response = await request.send();
  var responseData = await response.stream.toBytes();
  var responseString = String.fromCharCodes(responseData);
  print('================================');
  print("response was: ${responseString}");
  if (response.statusCode == 200) {
    var json_data = json.decode(responseString);
    for (var u in json_data) {
      print(u["response"]);
    }
  }

Upvotes: 2

Views: 4622

Answers (1)

kuhnroyal
kuhnroyal

Reputation: 7563

The http package uses dart:io in the background. That's Darts built in IO library.

When you make an https connection with the httppackage, it uses a SecurityContext (https://api.flutter.dev/flutter/dart-io/SecurityContext-class.html) that binds to the systems certificate/trust store. This works for most operating systems. If the operating system has no native trust store, it uses https://github.com/dart-lang/root_certificates.

Since you tagged the question with Flutter, both Android and iOS contain a native trust store which Dart uses.

If you want to trust different certificates you can manipulate the SecurityContext and add/remove trusted certificates.

This may also help you to understand how it works: https://api.flutter.dev/flutter/dart-io/HttpClient-class.html

Upvotes: 4

Related Questions