Flutter - How to get api response from a certification errored link - flutter

I'm trying to get an api from www.mocky.io, the issue is, when the app starts I'm getting an error like
E/flutter ( 846): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: HandshakeException: Handshake error in client (OS Error:
E/flutter ( 846): CERTIFICATE_VERIFY_FAILED: unable to get local issuer certificate(handshake.cc:354))
this is how I'm getting the response.
Future _fetchPost() async {
http.Response response = await http.get(url);
print(response.body);
setState(() {
var convertDatatoJson = json.decode(response.body);
debugPrint(convertDatatoJson.toString());
_data = convertDatatoJson['table_menu_list'];
});}
How can I verify the certificate for this api link for GET method?
I referred this issue, for POST method https://stackoverflow.com/a/54359013/11893608,
any other suggestions would be helpful

Change https to http
it will Resolve your Issue
Use this : http://www.mocky.io/v2/5dfccffc310000efc8d2c1ad

Try adding both http and www.
Example:
http://www.mocky.io/v2/5185415ba171ea3a00704eed
mocky.io may be using www to differentiate between subdomains. That is why its not worked when adding http only.

Related

Google Cloud Run TLS exception on dart

I've deployed a node.js service (REST API) in GCP Run and I'm having issues with the TLS handshake in Flutter.
Unhandled Exception: HandshakeException: Handshake error in client (OS Error: WRONG_VERSION_NUMBER(tls_record.cc:242))
The code that I'm trying to use is:
static Future<http.Response> get(String url, {Map<String, String>? headers}) async {
final _headers = await _addAuthHeader(headers); //appends the authorization header (not used right now)
final out = await http.get(Uri.parse(url), headers: _headers); //this throws the exception
return out;
}
In GCP Run I've haven't set anything about the security other than the allow unauthenticated calls so I should be using the managed TLS service.
I've tested in the following conditions:
curl https://<url> works ✅
curl --tls-max 1.2 https://<url> works ✅
curl --tls-max 1.1 https://<url> doesn't work ❌ (maybe Flutter doesn't support TLS 1.2?)
In dart (HTTPS call) using dio package doesn't work ❌
In dart (HTTPS call) using http package doesn't work ❌
In dart (HTTP call) works ✅
In chrome works ✅
I've tried the solutions proposed in these other posts:
Why i'm getting WRONG_VERSION_NUMBER when making a POST request to a HTTPS Server?
How to solve Flutter CERTIFICATE_VERIFY_FAILED error while performing a POST request?
Flutter on Android 7 CERTIFICATE_VERIFY_FAILED with LetsEncrypt SSL cert after Sept 30, 2021
System information:
Flutter version: 3.3.8
Dart version: 2.18.4
Flutter doctor: everything fine

Request http in flutter web

I want to post data to the server with dio library but when I send data, I get XMLHttpRequest error. The server programmer checked twice his codes and made sure to enable CORS in the server. I think this error occurs because the server is not HTTPS and the browser blocks the request. I use this code to send my data to the server:
Response response = await client
.post(theUrl,
options: Options(
validateStatus: (status) {
return status! < 500;
},
followRedirects: false,
headers: {
HttpHeaders.contentTypeHeader: "application/json",
HttpHeaders.acceptHeader: "*/*",
HttpHeaders.accessControlAllowOriginHeader:
"Access-Control-Allow-Origin, Accept",
}),
data: convert.json.encode(data))
.timeout(const Duration(seconds: 10));
My log is just:
DioError [DioErrorType.RESPONSE]: XMLHttpRequest error.
So, How can I ignore the certificate of the server host and send data to an HTTP URL in flutter web?
I'm almost sure that the problem is because your server doesn't have an https certificate.
A workaround is run / debug your application with flutter desktop.
You can also take a look at: How to solve flutter web api cors error only with dart code?

flutter: DioError [DioErrorType.response]: XMLHttpRequest error

I created an app and test it on emulator and andriod phone, i am using api's and nodejs based backend and mongodb database. it works fine when i test it on devices, but when i run it on chrome it sends me this error.
Error:
Error: Unsupported operation: NetworkInterface.list
DioError [DioErrorType.response]: XMLHttpRequest error.
here is my dio request code
Code:
calltimeInApi() async {
Dio dio=new Dio();
var data={
'username': getname,
'token': getaccesstoken,
};
timeOutButtonPressed=true;
await dio
.post(localhostUrlTimeIn,data: json.encode(data))
.then((onResponse)async {
getapitimein=onResponse.data['TimeIn'];
}).catchError((onerror){
print(onerror.toString());
});
}
please help where i am doing wrong!
There doesn't seem to be any issues on the code snippet that you've shared. If the HTTP Requests works fine on mobile devices but not on web, I suggest check the logs if you're getting CORS errors. A quick workaround on this issue is by running the web app with html renderer in debug.
flutter run -d chrome --web-renderer html

how to solve flutter dio connection timeout error

I/flutter (16260): DioError [DioErrorType.DEFAULT]: HttpException: Connection closed while receiving data, uri="URL".
I got this error while I post a req to the PHP backend which generate a file link .and send back the link as the response it takes about 5 seconds and more in postman. is there is any way to solve this problem with dio package or some other

How do I remove the Exception Error in running flutter?

fetchData()async{
res = await http.get(api);
Exception has occurred.
HandshakeException (HandshakeException: Handshake error in client (OS Error:
CERTIFICATE_VERIFY_FAILED: self signed certificate in certificate chain(handshake.cc:352)))
print(res.body);
drinks = jsonDecode(res.body)["drinks"];
setState(() {
});
Your error log clearly states that the error is not from flutter app side for api implementation.
Error is from web api side.
Contact your api person to resolve the server side issue.
Also try adding the internet permission in your manifest file,
<uses-permission android:name="android.permission.INTERNET" />
Then, in your terminal run the following command,
flutter clean
If it doesn't work i don't think any issues are from flutte side.