diff --git a/dio/CHANGELOG.md b/dio/CHANGELOG.md index a37b73cd0..e604d404f 100644 --- a/dio/CHANGELOG.md +++ b/dio/CHANGELOG.md @@ -2,7 +2,7 @@ ## Unreleased -*None.* +- Imply `List` as JSON content in `ImplyContentTypeInterceptor`. ## 5.0.2 diff --git a/dio/lib/src/interceptors/imply_content_type.dart b/dio/lib/src/interceptors/imply_content_type.dart index 9fa22e254..a26015abc 100644 --- a/dio/lib/src/interceptors/imply_content_type.dart +++ b/dio/lib/src/interceptors/imply_content_type.dart @@ -31,13 +31,13 @@ class ImplyContentTypeInterceptor extends Interceptor { final String? contentType; if (data is FormData) { contentType = Headers.multipartFormDataContentType; - } else if (data is Map || data is String) { + } else if (data is List || data is Map || data is String) { contentType = Headers.jsonContentType; } else { // Do not log in the release mode. if (!_kReleaseMode) { dev.log( - '${data.runtimeType} cannot be used' + '${data.runtimeType} cannot be used ' 'to imply a default content-type, ' 'please set a proper content-type in the request.', level: 900, diff --git a/dio/test/interceptor_test.dart b/dio/test/interceptor_test.dart index 4b8eed89f..4d752e1e5 100644 --- a/dio/test/interceptor_test.dart +++ b/dio/test/interceptor_test.dart @@ -376,6 +376,18 @@ void main() { expect(response.requestOptions.contentType, 'application/json'); }); + test('sets application/json for List instances', () async { + final dio = createDio(); + final response = await dio.get( + '/echo', + data: [ + {'hello': 'here'}, + {'hello': 'there'} + ], + ); + expect(response.requestOptions.contentType, 'application/json'); + }); + test('sets multipart/form-data for FormData instances', () async { final dio = createDio(); final response = await dio.get(