http get can't solve response error : Invalid media type

Created on 17 Jul 2018  ·  6Comments  ·  Source: dart-lang/http

_From @DuanTzXavier on July 17, 2018 7:32_

Come from flutter!on MacOS

Logs

07-14 01:59:51.068 2137-2153/top.basking.calculator I/flutter: 200
07-14 01:59:51.068 2137-2153/top.basking.calculator I/flutter: {connection: keep-alive, content-type: application/json; charset=utf-8;, transfer-encoding: chunked, date: Fri, 13 Jul 2018 17:59:15 GMT, server: nginx}
07-14 01:59:51.070 2137-2153/top.basking.calculator E/flutter: [ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
                                                               Error on line 1, column 33: Invalid media type: expected /[^()<>@,;:"\\\/[\]?={} \t\x00-\x1F\x7F]+/.
                                                               application/json; charset=utf-8;
                                                                                               ^
                                                               #0      wrapFormatException (package:http_parser/src/utils.dart:15:5)
                                                               #1      new MediaType.parse (package:http_parser/src/media_type.dart:46:12)
                                                               #2      _contentTypeForHeaders (package:http/src/response.dart:93:39)
                                                               #3      _encodingForHeaders (package:http/src/response.dart:86:22)
                                                               #4      Response.body (package:http/src/response.dart:27:22)
                                                               #5      ExchangeCalculatorScreenState.loadData (package:calculator/screens/ExchangeCalculatorScreen.dart:465:20)
                                                               <asynchronous suspension>

Code

var  dataURL = "http://api.k780.com?app=finance.rate&scur=CNY&tcur=GBP&appkey=35134&sign=fb020c3129435bb5ff21b7113e9cb1c1&format=json";
    print(dataURL);
    var response = await http.get(
        dataURL);
    print(response.statusCode);
    print(response.headers);
    print(response.body);

can't resolve response.body

but when I use UTF8Codec class resolve bodybytes ,it works!

print(Utf8Codec().decode(response.bodyBytes));

here is success log:

{"success":"1","result":{"status":"ALREADY","scur":"CNY","tcur":"GBP","ratenm":"人民币/英镑","rate":"0.113045","update":"2018-07-13 23:28:01"}}

here is part of my flutter doctor -v

[✓] Flutter (Channel beta, v0.5.1, on Mac OS X 10.13.5 17F77, locale en-NZ)
    • Flutter version 0.5.1 at /Users/tzduan/development/flutter
    • Framework revision c7ea3ca377 (6 weeks ago), 2018-05-29 21:07:33 +0200
    • Engine revision 1ed25ca7b7
    • Dart version 2.0.0-dev.58.0.flutter-f981f09760

by the way, How to specific config dart sdk in flutter?
flutter always use cache dart sdk....

_Copied from original issue: dart-lang/sdk#33881_

closed-as-intended type-enhancement

Most helpful comment

As a workaround, you can use utf8.decode(response.bodyBytes);

All 6 comments

_From @zoechi on July 17, 2018 8:49_

How to specific config dart sdk in flutter?
flutter always use cache dart sdk....

There is no way to chose a different SDK version for a specific Flutter version. These versions are tightly bound together.

This seems to be caused by an invalid media type declaration:

application/json; charset=utf-8;

According to RFC 7231 the grammar for media types the type/subtype pair followed by zero or more "; parameter=value" pairs, hence the last ";" is invalid.

This is a bug on the server sending the media type (in the Content-Type header) and that should be fixed, which has nothing to do with Dart http.
However, I do think there might be some room for improvement on robustness on our side to make the parser a bit more forgiving - though it is currently technically correct..

As a workaround, you can use utf8.decode(response.bodyBytes);

I'll be working around this, as I don't have quick control over the remote service I'm interacting with.

Forgiving trailing semicolon might be nice.

Is the issue resolved ?. I am stuck at this step.

The issue is not resolved fully So Try to Use HttpClient

Was this page helpful?
0 / 5 - 0 ratings