dio
dio copied to clipboard
[Exceptions] Invalid media type: expected no more input: charset=gbk,utf8
New Issue Checklist
- [鈭?] I have searched for a similar issue in the project and found none
Issue Info
Info | Value | |
---|---|---|
Platform Name | windows | |
Platform Version | win10 | |
Dio Version | 4.0.4 | |
VSCode | VSCode 1.65.1 | |
Repro rate | 100% | |
Repro with our demo prj | yes | |
Demo project link |
Issue Description and Steps
鎴戜娇鐢?nginx-1.20.2 鎼簡涓€涓畝鏄搘eb鏈嶅姟鍣紝鍦ㄥ悜鍏跺彂閫佽姹傛椂锛屾姤鍛婁簡寮傚父
DioError (DioError [DioErrorType.other]: Error on line 1, column 23: Invalid media type: expected no more input. 鈺?1 鈹?text/html; charset=gbk,utf8 |.....................................................^ 鈺?/p>
鍦╪ginx鐨勯厤缃枃浠朵腑閰嶇疆gbk鏄负浜嗗鐞嗗缂栫爜璺緞鐨勬儏鍐碉紝dio鏄惁鏀寔杩欓」鍔熻兘锛熸垨鑰呰鏈夋病鏈変粈涔堝姙娉曞鐞嗚繖绉嶉棶棰橈紵
Dio thorw exception when I request to a nginx-1.20.2 server. It set charset=gbk,utf8 to resolve some problem in multi-charset.
DioError (DioError [DioErrorType.other]: Error on line 1, column 23: Invalid media type: expected no more input. 鈺?1 鈹?text/html; charset=gbk,utf8 |....................................................^ 鈺?/p>
May Dio support this feature, or something else I can resolve it ?
@zhangzy7
This seems to be caused by an invalid media type declaration: text/html; charset=gbk,utf8
According to RFC 7231 the grammar for media types the type/subtype pair followed by zero or more "; parameter=value" pairs.
https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.1.2
This is where the error occurred. https://github.com/flutterchina/dio/blob/6d04ec95d951bdecd19a2939d94de217d2d1ff7d/dio/lib/src/transformer.dart#L172
https://pub.dev/documentation/http_parser/latest/http_parser/MediaType/MediaType.parse.html
Parses a media type.
This will throw a FormatError if the media type is invalid.
MediaType.parse('text/html; charset=gbk,utf8');
Error on line 1, column 23: Invalid media type: expected no more input.
鈺?1 鈹?text/html; charset=gbk,utf8
鈹? ^
鈺?
import 'package:http_parser/http_parser.dart';
import 'package:test/test.dart';
void main() {
group('A group of tests', () {
test('charset=gbk,utf8', () {
final type = MediaType.parse('text/html; charset=gbk,utf8');
expect(type.parameters['charset'], 'utf8');
});
test('charset=utf8', () {
final type = MediaType.parse('text/html; charset=utf8');
expect(type.parameters['charset'], 'utf8');
});
test('charset=gbk', () {
final type = MediaType.parse('text/html; charset=gbk');
expect(type.parameters['charset'], 'gbk');
});
test('two parameters', () {
final type = MediaType.parse('text/html; charset=gbk;foo=bar');
expect(type.parameters['charset'], 'gbk');
expect(type.parameters['foo'], 'bar');
});
});
}