body: <FormatException: Missing extension byte (at offset 2)>
sdk: '>=3.4.0 <4.0.0' http: ^1.2.1
I encountered an issue with an HTTP request in version 1.2.1. Here is the original function I used:
Future<Response> getRequest(String url) async {
var headers = {
'Authorization': 'Bearer $token',
'Accept': '/',
'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive'
};
final apiUrl = Uri.parse(base_url + url);
// return await get(apiUrl, headers: headers);
final response = await retry(
// Make a GET request
() => get(apiUrl, headers: headers).timeout(const Duration(seconds: 15)),
// Retry on SocketException or TimeoutException
retryIf: (e) => e is SocketException || e is TimeoutException,
);
return response;
}
To resolve the issue, I made the following change:
Future<Response> getRequest(String url) async {
var headers = {
'Authorization': 'Bearer $token',
'Accept': '/',
// 'Accept-Encoding': 'gzip, deflate, br',
'Connection': 'keep-alive'
};
final apiUrl = Uri.parse(base_url + url);
// return await get(apiUrl, headers: headers);
final response = await retry(
// Make a GET request
() => get(apiUrl, headers: headers).timeout(const Duration(seconds: 15)),
// Retry on SocketException or TimeoutException
retryIf: (e) => e is SocketException || e is TimeoutException,
);
return response;
}
By simply removing 'Accept-Encoding': 'gzip, deflate, br' from the headers, the body of the response was understood correctly again.
i have same error
http: ^1.2.1
url:https://m.gaonaojin.com
header: 'Accept-Encoding': 'gzip, deflate, br'
error:FormatException: Unexpected extension byte (at offset 5)
This is because this http lib DO NOT deal with the br encoding !!!
Workaround:
set the 'Accept-Encoding': 'gzip, deflate'
this solved my problem.