http v1.6.0 breaking change: charset parameter is not added for json

This commit is contained in:
Ankit Mahato
2026-03-08 04:36:11 +05:30
parent b213580c80
commit d2894bbe28
2 changed files with 9 additions and 18 deletions

View File

@@ -119,7 +119,6 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequestV1(
method: authenticatedRequestModel.method.name.toUpperCase(),
headers: headers,
body: body,
overrideContentType: overrideContentType,
);
final streamed = await client.send(request);
response = await http.Response.fromStream(streamed);
@@ -182,20 +181,12 @@ http.Request prepareHttpRequest({
required String method,
required Map<String, String> headers,
required String? body,
bool overrideContentType = false,
}) {
var request = http.Request(method, url);
if (headers.getValueContentType() != null) {
request.headers[HttpHeaders.contentTypeHeader] = headers
.getValueContentType()!;
if (!overrideContentType) {
headers.removeKeyContentType();
}
}
if (body != null) {
request.body = body;
headers[HttpHeaders.contentLengthHeader] = request.bodyBytes.length
.toString();
headers[HttpHeaders.contentLengthHeader] =
request.bodyBytes.length.toString();
}
request.headers.addAll(headers);
return request;
@@ -398,7 +389,6 @@ Future<http.StreamedResponse> makeStreamedRequest({
method: requestModel.method.name.toUpperCase(),
headers: headers,
body: body,
overrideContentType: overrideContentType,
);
streamedResponse = await client.send(request);
}

View File

@@ -192,30 +192,31 @@ void main() {
});
});
group('Testing overrideContentType functionality', () {
group(
'Testing overrideContentType functionality not required due to http v1.6.0 breaking change',
() {
test('overrideContentType is true', () async {
final request = prepareHttpRequest(
url: Uri.parse('https://www.example.com'),
method: 'POST',
body: 'Hello',
headers: {'content-type': 'application/json'},
overrideContentType: true,
);
expect(request.headers['content-type'], 'application/json');
});
test('overrideContentType is false', () async {
test('; charset=utf-8 is not appended due to http v1.6.0 breaking change',
() async {
final request = prepareHttpRequest(
url: Uri.parse('https://www.example.com'),
method: 'POST',
body: 'Hello',
headers: {'content-type': 'application/json'},
overrideContentType: false,
);
expect(request.headers['content-type'], isNot('application/json'));
expect(request.headers['content-type'], 'application/json');
expect(
request.headers['content-type'],
'application/json; charset=utf-8',
isNot('application/json; charset=utf-8'),
);
});
});