4

I'm using HttpClient from dart (dart:io package, NOT dart:http) and I'd like to send an HTTPS request. Is there a way to do that? I can't seem to find a method that would allow me that.

Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
markovuksanovic
  • 15,676
  • 13
  • 46
  • 57

3 Answers3

4
new HttpClient().getUrl(Uri.parse('https://www.somedomain.com'));
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
1
HttpClient client = new HttpClient();
client.getUrl(Uri.parse("http://www.example.com/"))
    .then((HttpClientRequest request) {
      // Optionally set up headers...
      // Optionally write to the request object...
      // Then call close.
      ...
      return request.close();
    })
    .then((HttpClientResponse response) {
      // Process the response.
      ...
    });

Reft: https://api.dart.dev/stable/2.13.1/dart-io/HttpClient-class.html

Ihda
  • 11
  • 2
  • To add to what Ihda answered, you can also use this same function and add headers and a body as needed, you can also use the post method to post with the same format – Chris Sanchez Feb 27 '23 at 14:57
0

The steps of sending HTTPS request is the same as HTTP in dart/flutter, one thing you have to add is to allow self signed certificates to handle badCertificateCallback, add this to your HttpClient:

var httpClient = HttpClient();
      httpClient.badCertificateCallback =
          ((X509Certificate cert, String host, int port) =>
              true); // Allow self signed certificates

Ref: https://medium.com/@reme.lehane/flutter-using-self-signed-ssl-certificates-in-development-c3fe2d104acf

Mohamed Dernoun
  • 776
  • 5
  • 13