Occasionally a ClientException is thrown with no error message, which makes debugging difficult. My app has an error handler which catches the exception with onError: (error, stackTrace). The stack trace is always there, but the error parameter is sometimes blank. I have not been able to replicate the error myself, but I get several error reports each day. How should this exception be handled?
The stack trace is always:
#0 IOClient.send (package:http/src/io_client.dart:65)
<asynchronous suspension>
dart-lang/http#1 RetryClient.send (package:http_retry/http_retry.dart:97)
<asynchronous suspension>
dart-lang/http#2 BaseClient._sendUnstreamed (package:http/src/base_client.dart:169)
<asynchronous suspension>
dart-lang/http#3 BaseClient.post (package:http/src/base_client.dart:54)
...or...
dart-lang/http#3 BaseClient.get (package:http/src/base_client.dart:32)
Looking at the io_client.dart code I don't see any obvious cause of empty errors like that.
My best guess based on nothing else would be that something with the connection or response is malfunctioning.
Do you have some minimal example code showing how this is triggered?
Thanks.
I have similar or even same error.
E/flutter (27875): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception:
E/flutter (27875): #0 IOClient.send (package:http/src/io_client.dart:65:7)
E/flutter (27875): <asynchronous suspension>
E/flutter (27875): dart-lang/http#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:169:38)
E/flutter (27875): <asynchronous suspension>
E/flutter (27875): dart-lang/http#2 BaseClient.post (package:http/src/base_client.dart:54:7)
E/flutter (27875): dart-lang/http#3 post.<anonymous closure> (package:http/http.dart:70:16)
E/flutter (27875): dart-lang/http#4 _withClient (package:http/http.dart:166:20)
E/flutter (27875): <asynchronous suspension>
E/flutter (27875): dart-lang/http#5 post (package:http/http.dart:69:5)
E/flutter (27875): dart-lang/http#6 _ConfigScreenState.getUserToken (package:optimo_offline/screens/config_screen.dart:267:31)
E/flutter (27875): <asynchronous suspension>
E/flutter (27875): dart-lang/http#7 _ConfigScreenState.build.<anonymous closure> (package:optimo_offline/screens/config_screen.dart:192:25)
E/flutter (27875): dart-lang/http#8 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:635:14)
E/flutter (27875): dart-lang/http#9 _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:711:32)
E/flutter (27875): dart-lang/http#10 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:182:24)
E/flutter (27875): dart-lang/http#11 TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:365:11)
E/flutter (27875): dart-lang/http#12 TapGestureRecognizer.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:275:7)
E/flutter (27875): dart-lang/http#13 PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:455:9)
E/flutter (27875): dart-lang/http#14 PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:75:13)
E/flutter (27875): dart-lang/http#15 PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:102:11)
E/flutter (27875): dart-lang/http#16 _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:218:19)
E/flutter (27875): dart-lang/http#17 _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:198:22)
E/flutter (27875): dart-lang/http#18 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:156:7)
E/flutter (27875): dart-lang/http#19 _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:102:7)
E/flutter (27875): dart-lang/http#20 _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:86:7)
E/flutter (27875): dart-lang/http#21 _rootRunUnary (dart:async/zone.dart:1136:13)
E/flutter (27875): dart-lang/http#22 _CustomZone.runUnary (dart:async/zone.dart:1029:19)
E/flutter (27875): dart-lang/http#23 _CustomZone.runUnaryGuarded (dart:async/zone.dart:931:7)
E/flutter (27875): dart-lang/http#24 _invoke1 (dart:ui/hooks.dart:250:10)
E/flutter (27875): dart-lang/http#25 _dispatchPointerDataPacket (dart:ui/hooks.dart:159:5)
No exception text, no error message, no response. I have two APIs and first one works fine but second doesn't. The only different between them is url.
Example url (fake ones) to show a difference:
working one: http://api.troncomputers.pl/api/data
not working one: https://api.tron-computers/api/data
I'm sure that this is not a https vs http issue. I've checked with fake API without - character in url and works fine.
ver. http: ^0.12.0+2
It looks like this would be caused by an HttpException with a blank message.
https://github.com/dart-lang/http/blob/7774121a406fedb0502fce7cc224476c1ac78469/lib/src/io_client.dart#L65
Can you check how these requests behave when you use the SDK classes from dart:io directly instead of going through this package? It's likely we need to find what causes the exception with a blank message and fix that in the SDK...
I'm not sure if I'm doing it right. This is the code I used:
HttpClient client = HttpClient();
client.getUrl(Uri.parse(panelUrl)).then((HttpClientRequest request) {
return request.close();
}).then((HttpClientResponse response) {
response.transform(utf8.decoder).listen((contents) {
print(contents);
});
});
Exception I get:
E/flutter ( 5121): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: HttpException: , uri = https://api.tron-computers/api/data/getzadaniaopiekuna
E/flutter ( 5121): null
I should get StatusCode 401 Unauthorized.
Do you really need the correct API url for debugging? I am not quite sure about security with putting that here.
馃
Unless you have some unusual DNS setup on your network and/or device, that is expected to fail with a DNS resolve error since api.tron-computers is not a valid address.
No request will be sent to your server, and no 401 will be returned.
The API is behind an Angular website. The whole project is Angular + .NET Core API but I need to connect to that API with Android app. I can open an Angular website on emulator and everything is fine and working great. The HttpException is thrown on emulator and on normal device through WIFI and LTE. If an URL is not valid I'm getting:
E/flutter (10528): [ERROR:flutter/lib/ui/ui_dart_state.cc(148)] Unhandled Exception: SocketException: Failed host lookup: 'api.tron-computers' (OS Error: No address associated with hostname, errno = 7)
E/flutter (10528): null
I didn't specified any custom DNS configuration on any devices.
I've tested with ApiTester and I'm getting HTTP/1.1 401 Unauthorized
The troubling part here is Unhandled Exception: HttpException: ,
For some reason the message is blank.
I'll transfer this to the SDK repo since the underlying problem is somewhere in the http handling.
I have this error on production server endpoint (line numbers do not correspond to original, as I've added dozen of prints):
flutter: Exception occured: stackTrace: #0 IOClient.send (package:http/src/io_client.dart:70:7)
<asynchronous suspension>
#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:178:38)
<asynchronous suspension>
#2 BaseClient.get (package:http/src/base_client.dart:32:7)
...
as I cannot share URL on public, I may only say that it works via browser or postman/Insomnia.
Typical answer from server should be like this:
HTTP/1.1 200 OK
Cache-Control: private
Transfer-Encoding: chunked
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Sat, 19 Oct 2019 23:46:55 GMT
Maybe this will help somehow?
Url scheme is HTTPS and I'm using port 888.
I'm also having this issue with api requests on an IIS server that has some MS .NET service. It's working fine on postman/android/ios, but not here. Could it be related to some non standard formatted response from the server, unexpected weird headers?
Any updates in this thread?
I'm also having this issue with api requests on an IIS server that has some MS .NET service. It's working fine on postman/android/ios, but not here. Could it be related to some non standard formatted response from the server, unexpected weird headers?
Mee too, anyone can help?
same problem but always happened not occasional (works normally using curl, postman, or nodejs version but not with dart http or dio package), tested with http 0.12.1:
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
void main(List<String> arguments) {
print('hit api start');
hitApi();
print('hit api end');
}
void hitApi() {
http.post(
// API_URL + API_Login,
'https://xx.co.id:2020/xx/mobileapi/login',
headers: {
// "Accept": 'application/json',
"Content-Type": "application/json",
"x-api-key": "x.y.jbOfRigeCCFN2Lq0O2xVeIcTHNWpQBlI0ctmDiQBdBg",
},
body: json.encode({
"nik": "on ff",
}),
).then((response) {
print("status code");
print(response.statusCode);
print("status body");
print(response.body);
}); // .catchError((e) { print(e); });
}
the error:
Unhandled exception:
#0 IOClient.send (package:http/src/io_client.dart:64:7)
<asynchronous suspension>
#1 BaseClient._sendUnstreamed (package:http/src/base_client.dart:171:38)
#2 BaseClient.post (package:http/src/base_client.dart:56:5)
#3 post.<anonymous closure> (package:http/http.dart:70:34)
#4 _withClient (package:http/http.dart:167:20)
#5 post (package:http/http.dart:70:3)
#6 hitApi (file:///tmp/test.dart:13:3)
#7 main (file:///tmp/test.dart:8:3)
#8 _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:299:32)
#9 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
We are facing the same problem, it can always reproduce with HttpClient(dart:io packet) and our server(IIS with .net core service), and the endpoint is work fine on Browser, Postman, curl, Android, iOS native project, but just dart sdk client can't receive the response normally.
import 'dart:convert';
import 'dart:io';
void testHttpGet() async {
var client = HttpClient();
var uri = Uri.parse(url);
try {
print('Test started');
var request = await client.getUrl(uri);
print('Request prepared');
var response = await request.close();
print('Response received');
var content = await response.transform(utf8.decoder).first;
print('Http get success:\n$content');
} catch(e, stacktrace) {
print('Http exception:\nInstance of: ${e.runtimeType}\nMessage: ${e.message}\nUri: ${e.uri}');
print('Http stacktrace:\n$stacktrace');
}
}
Test started
Request prepared
Http exception:
Instance of: HttpException
Message:
Uri: url
Http stacktrace:
< HTTP/1.1 200 OK
< Transfer-Encoding: chunked
< Content-Type: text/plain; charset=utf-8
< Server: Microsoft-IIS/10.0
< X-Powered-By: ASP.NET
$ dart --version
Dart VM version: 2.8.1 (stable) (Thu Apr 30 09:25:21 2020 +0200) on "macos_x64"
We are facing the same problem, it can always reproduce with HttpClient(dart:io packet) and our server(IIS with .net core service), and the endpoint is work fine on Browser, Postman, curl, Android, iOS native project, but just dart sdk client can't receive the response normally.
We can provide the endpoint for test, please help us to resolve this issue, thanks a lot.
Target Endpoint(For Safty I alrady remove this url)
/api/Company/GetCompanies
Test Function
import 'dart:convert'; import 'dart:io'; void testHttpGet() async { var client = HttpClient(); var uri = Uri.parse('https://xxxxxxxx/api/Company/GetCompanies'); try { print('Test started'); var request = await client.getUrl(uri); print('Request prepared'); var response = await request.close(); print('Response received'); var content = await response.transform(utf8.decoder).first; print('Http get success:\n$content'); } catch(e, stacktrace) { print('Http exception:\nInstance of: ${e.runtimeType}\nMessage: ${e.message}\nUri: ${e.uri}'); print('Http stacktrace:\n$stacktrace'); } }Test Log
Test started Request prepared Http exception: Instance of: HttpException Message: Uri: https://xxxxxx/api/Company/GetCompanies Http stacktrace:About This Service
< HTTP/1.1 200 OK < Transfer-Encoding: chunked < Content-Type: text/plain; charset=utf-8 < Server: Microsoft-IIS/10.0 < X-Powered-By: ASP.NETDart SDK Info
$ dart --version Dart VM version: 2.8.1 (stable) (Thu Apr 30 09:25:21 2020 +0200) on "macos_x64"
Hi,
this issue is because of your response header "Transfer-Encoding:chunked",
Dart isn't support "Transfer-Encoding", you must give the content-length for your response header and "Transfer-Encoding" would be removed automatically.
@changyuhao625 Hi, I tried your solution. But the error still there.
Test Log
flutter: httpclient Error: HttpException: , uri = https://www.xxxxxxx.com/api/spacebar-widgets/1
flutter: Http exception:
Instance of: HttpException
Message:
Uri: https://www.xxxxxxx.com/api/spacebar-widgets/1
flutter: Http stacktrace:
flutter: StackTrace:

Dart SDK Info
Dart SDK version: 2.12.0-62.0.dev (dev) (Thu Nov 19 10:08:53 2020 -0800) on "macos_x64"
I've the same issue. The issue started as soon as we switched from HTTP to HTTPS on IIS 10.0. The problem is not with the generic HTTPS since the other HTTPS end-points e.g. Azure/AWS works well.
Problem is flutter http client does not fully support http 1.1, more specifically it does not support chunked responses. Solution for us was to add chunked_transfer_encoding off; in our webserver (nginx) in the php/fastcgi location in the config file.
I'm also having this issue with api requests on an IIS server that has some MS .NET service. It's working fine on postman/android/ios, but not here. Could it be related to some non standard formatted response from the server, unexpected weird headers?
I think it's just with IIS server. I've got the same issue by connecting via a Https post and I cannot go through it. Always received a HttpException with blank message but the ConnectionState is done.
I just had the same error captured in Sentry, blank HttpException without any message and so on. Not using IIS but the OP probably wasn't either since he said this happened occasionally.
Interestingly prior to the exception but after the request, there is memory pressure logged for the app (Flutter obviously).
So this is what happened for me:
Most helpful comment
Hi,
this issue is because of your response header "Transfer-Encoding:chunked",
Dart isn't support "Transfer-Encoding", you must give the content-length for your response header and "Transfer-Encoding" would be removed automatically.