Skip to content

Commit 286dbac

Browse files
committed
Fix issue foss42#630: Unable to override Content-Type header charset
1 parent 5157943 commit 286dbac

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

packages/apidash_core/lib/models/http_request_model.dart

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class HttpRequestModel with _$HttpRequestModel {
2727
@Default(ContentType.json) ContentType bodyContentType,
2828
String? body,
2929
String? query,
30+
@Default(false) bool useRawContentType,
3031
List<FormDataModel>? formData,
3132
}) = _HttpRequestModel;
3233

@@ -40,7 +41,14 @@ class HttpRequestModel with _$HttpRequestModel {
4041
List<NameValueModel>? get enabledParams =>
4142
getEnabledRows(params, isParamEnabledList);
4243

43-
Map<String, String> get enabledHeadersMap => rowsToMap(enabledHeaders) ?? {};
44+
Map<String, String> get enabledHeadersMap {
45+
final map = rowsToMap(enabledHeaders) ?? {};
46+
if (useRawContentType && map.containsKey(HttpHeaders.contentTypeHeader)) {
47+
// Use the raw Content-Type header value without charset
48+
map[HttpHeaders.contentTypeHeader] = map[HttpHeaders.contentTypeHeader]!.split(";")[0];
49+
}
50+
return map;
51+
}
4452
Map<String, String> get enabledParamsMap => rowsToMap(enabledParams) ?? {};
4553

4654
bool get hasContentTypeHeader => enabledHeadersMap.hasKeyContentType();

packages/apidash_core/lib/services/http_service.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
4949
body = requestBody;
5050
headers[HttpHeaders.contentLengthHeader] =
5151
contentLength.toString();
52-
if (!requestModel.hasContentTypeHeader) {
53-
headers[HttpHeaders.contentTypeHeader] =
54-
requestModel.bodyContentType.header;
55-
}
52+
if (!requestModel.hasContentTypeHeader) {
53+
headers[HttpHeaders.contentTypeHeader] = requestModel.useRawContentType
54+
? requestModel.bodyContentType.header.split(";")[0]
55+
: requestModel.bodyContentType.header;
56+
}
5657
}
5758
}
5859
if (isMultiPartRequest) {
@@ -103,9 +104,11 @@ Future<(HttpResponse?, Duration?, String?)> sendHttpRequest(
103104
if (contentLength > 0) {
104105
body = requestBody;
105106
headers[HttpHeaders.contentLengthHeader] = contentLength.toString();
106-
if (!requestModel.hasContentTypeHeader) {
107-
headers[HttpHeaders.contentTypeHeader] = ContentType.json.header;
108-
}
107+
if (!requestModel.hasContentTypeHeader) {
108+
headers[HttpHeaders.contentTypeHeader] = requestModel.useRawContentType
109+
? requestModel.bodyContentType.header.split(";")[0]
110+
: requestModel.bodyContentType.header;
111+
}
109112
}
110113
}
111114
response = await client.post(

test/content_type_test.dart

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'dart:io';
2+
import 'package:flutter_test/flutter_test.dart';
3+
4+
void main() {
5+
group('Content-Type Header Tests', () {
6+
test('useRawContentType removes charset from Content-Type header', () {
7+
// Simulate the behavior directly
8+
final contentTypeWithCharset = 'application/json; charset=utf-8';
9+
final contentTypeWithoutCharset = contentTypeWithCharset.split(';')[0];
10+
11+
// Verify the splitting works
12+
expect(contentTypeWithoutCharset, equals('application/json'));
13+
14+
// Simulate the actual fix logic
15+
final useRawContentType = true;
16+
final map = {'Content-Type': 'application/json; charset=utf-8'};
17+
18+
if (useRawContentType && map.containsKey('Content-Type')) {
19+
map['Content-Type'] = map['Content-Type']!.split(';')[0];
20+
}
21+
22+
// Verify the result
23+
expect(map['Content-Type'], equals('application/json'));
24+
25+
print('Test PASSED: Content-Type header without charset is: ${map['Content-Type']}');
26+
});
27+
});
28+
}

0 commit comments

Comments
 (0)