I have a Flutter application which is attempting to connect to a server API using the Dio package. The server uses a self-signed certificate, which means using a Dio workaround to verify the certificate in order to connect to the server (see https://pub.dev/packages/dio#https-certificate-verification).
Below is what I have so far:
String url = 'https://...'; // Server API URL
Map<String, dynamic> data = ... // Request body
String PEM = "XXXXX"; // Certificate content
Dio dio = Dio();
(dio.httpClientAdapter as DefaultHttpClientAdapter).onHttpClientCreate = (client) {
client.badCertificateCallback=(X509Certificate cert, String host, int port){
if (cert.pem == PEM) {
// Verify the certificate
return true;
}
return false;
};
};
Response response = await dio.post(url, data: data);
However this throws an HttpException:
HttpException: Failed to parse HTTP, uri = https://... // Server API url
No other information is shown in the Exception. The value of response.data is null.
It is important to note that the server is in fact responding with data - so this appears to be a Flutter / client side issue. The server response is a standard object of String fields and values:
{
"status": "ok",
"token": "...",
"field1": "...",
"field2": "...",
"field3": "...",
...
}
How do I fix this issue?