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.
3 Answers
new HttpClient().getUrl(Uri.parse('https://www.somedomain.com'));
7 Comments
Günter Zöchbauer
And, did it work? I think in this case you should just delete the question.
markovuksanovic
No, because there is no method with the signature that would allow me to specify url in this form.
markovuksanovic
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.
Justin Fagnani
Can you self-answer this with the
openUrl answer so future Darters can see it?Günter Zöchbauer
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.
|
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
Comments
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
HttpClient.open(url)?