http
http copied to clipboard
http get can't solve response error : Invalid media type
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
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.
Experiencing the same issue where a web server causes an error similar to this:
╷
1 │ application/json; url=http://example.org/a#; charset=utf-8
│ ^
╵
I also got a an error error on line 1 column 5 invalid media type expected "/"
http.get on 'https://tass.com/rss/v2.xml'
Invalid media type: expected no more input. ╷ 1 │ application/rss+xml, application/rdf+xml;q=0.8, application/atom+xml;q=0.6, application/xml;q=0.4, text/xml;q=0.4; charset=utf-8
curl output:
< HTTP/2 200 < server: nginx < date: Fri, 08 Sep 2023 05:44:20 GMT < content-type: application/rss+xml, application/rdf+xml;q=0.8, application/atom+xml;q=0.6, application/xml;q=0.4, text/xml;q=0.4; charset=utf-8 < set-cookie: tass_uuid=14FC4D84-F356-4C1A-9889-E863DE956405; Path=/; Expires=Sat, 07-Sep-24 05:43:45 GMT < x-frame-options: SAMEORIGIN < x-xss-protection: 1; mode=block < x-content-type-options: nosniff