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.
Asked
Active
Viewed 8,917 times
4
-
why not try `HttpClient.open(url)`? – Daniel Robinson Feb 11 '14 at 13:40
-
I don't see a method with that signature.... – markovuksanovic Feb 11 '14 at 13:55
-
Sorry, didn't even see the methods with openUrl. I was expecting to be able to pass in scheme to open. The other methods were not suitable as I need other HTTP verbs too (i.e. PUT and PATCH). – markovuksanovic Feb 11 '14 at 23:24
3 Answers
4
new HttpClient().getUrl(Uri.parse('https://www.somedomain.com'));

Günter Zöchbauer
- 623,577
- 216
- 2,003
- 1,567
-
And, did it work? I think in this case you should just delete the question. – Günter Zöchbauer Feb 11 '14 at 15:29
-
No, because there is no method with the signature that would allow me to specify url in this form. – markovuksanovic Feb 11 '14 at 20:59
-
Sorry, there was a method, which I didn't see. I was too much focused around how to pass scheme into open method. There's another method called openUrl which does the job. Nevertheless, it's weird that you can't pass scheme into open method. – markovuksanovic Feb 11 '14 at 23:59
-
Can you self-answer this with the `openUrl` answer so future Darters can see it? – Justin Fagnani Feb 14 '14 at 03:55
-
Sorry didn't know that the API is so different than in the browser (didn't use it for a while). Now I understand why it was not so obvious. – Günter Zöchbauer Feb 14 '14 at 08:11
-
@GünterZöchbauer it didn't work on https, i got this error CERTIFICATE_VERIFY_FAILED: self signed certificate(handshake.cc:354)) – Mohamed Dernoun Mar 23 '20 at 17:01
-
@MohamedDernoun https://medium.com/@reme.lehane/flutter-using-self-signed-ssl-certificates-in-development-c3fe2d104acf should help – Günter Zöchbauer Mar 23 '20 at 18:07
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

Mohamed Dernoun
- 776
- 5
- 13