http icon indicating copy to clipboard operation
http copied to clipboard

body: <FormatException: Missing extension byte (at offset 2)>

Open carlospo opened this issue 1 year ago • 1 comments

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. http_issue

carlospo avatar Jun 14 '24 13:06 carlospo

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)

LandChanning avatar Jul 22 '24 03:07 LandChanning

This is because this http lib DO NOT deal with the br encoding !!!
Workaround: set the 'Accept-Encoding': 'gzip, deflate'

this solved my problem.

CuteLemon avatar Dec 03 '24 04:12 CuteLemon