Reputation: 920
I just started playing around with Flutter, and am now trying to do some simple HTTP(s) requests. I've always used Fiddler with proxy to debug requests send from android apps, but the requests send from Flutter don't seem to show.
Is there any way I can get Fiddler to show Flutter requests, or is there some other HTTP Requests debugging tool/monitor I can/should use?
Upvotes: 6
Views: 5088
Reputation: 13216
In general, if you're facing this issue it's means that your Flutter application is either ignoring the system proxy settings for some reason, or that it doesn't trust Fiddler's HTTPS certificate.
You can usually tell the difference by using the app and checking its logs: if the app works OK and the logs are fine, it's probably ignoring Fiddler entirely. If the app can't connect to the internet, especially if there are security/TLS/certificate warnings in logcat, then it probably doesn't trust the certificate.
If the proxy isn't trusted, you need to either do that in your code, or use configure a separate app that can force proxy settings for you by acting as a VPN. If the certificate isn't trusted, you'll need to configure that in your app's network security settings.
Fiddler isn't really a great option for Android though. You might have better luck with a tool with Android support built-in - I've been building exactly that, it's an open-source project called HTTP Toolkit which can do this for all Android apps, including anything built with Flutter:
It does all the VPN proxy-enforcing work for you, with one-click setup and no code changes, and it can automatically handle certificate setup too (even system-level certificates, on emulators & rooted devices). That should remove both ways you can get into this situation.
Otherwise it's very similar to Fiddler: an intercepting proxy that lets you inspect & rewrite HTTPS from any clients you like.
Upvotes: 3
Reputation: 7628
I was able to get Charles Proxy to work with HttpClient using the following snippets:
localhost can be the ip address for your computer like 192.168.1.77
HttpClient client = HttpClient();
client.findProxy = (uri) {
return "PROXY localhost:3128;";
};
A more full implementation:
static HttpClient getHttpClient() {
HttpClient client = new HttpClient()
..findProxy = (uri) {
return "PROXY 192.168.1.199:8888;";
}
..badCertificateCallback =
((X509Certificate cert, String host, int port) => true);
return client;
}
More information and solution for other HTTP clients can also be found here:
https://github.com/flutter/flutter/issues/20376
Upvotes: 2
Reputation: 920
I did some more tests with Drony and I got it to work. Still one problem, I couldn't send HTTPS requests anymore since Dart/Flutter doesn't have an easy way to trust all SSL certificates. I could debug HTTP requests at this point.
My API only support HTTPS requests, so in my Flutter app I request the HTTP version and I forward it to the HTTPS API via Fiddler: (following code goes in the OnBeforeRequest function)
if (!oSession.isHTTPS && oSession.HostnameIs("api.xxxx.me")){
oSession.fullUrl = "https://" + "api.xxxx.me" + oSession.PathAndQuery;
}
Upvotes: 2
Reputation: 44056
Haven't tried this, but perhaps you could subclass the HttpClient class, and override the methods you want to trace, and use that when you want to get tracing?
Upvotes: 0