Skip to content

Commit

Permalink
🚀 Imply List<Map> as JSON content (#1737)
Browse files Browse the repository at this point in the history
Resolves #1736.

### New Pull Request Checklist

- [x] I have read the
[Documentation](https://pub.dev/documentation/dio/latest/)
- [x] I have searched for a similar pull request in the
[project](https://github.com/cfug/dio/pulls) and found none
- [x] I have updated this branch with the latest `main` branch to avoid
conflicts (via merge from master or rebase)
- [x] I have added the required tests to prove the fix/feature I'm
adding
- [x] I have updated the documentation (if necessary)
- [x] I have run the tests without failures
- [x] I have updated the `CHANGELOG.md` in the corresponding package
  • Loading branch information
AlexV525 authored Mar 14, 2023
1 parent be2b0f5 commit 4204004
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dio/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## Unreleased

*None.*
- Imply `List<Map>` as JSON content in `ImplyContentTypeInterceptor`.

## 5.0.2

Expand Down
4 changes: 2 additions & 2 deletions dio/lib/src/interceptors/imply_content_type.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Map> || 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,
Expand Down
12 changes: 12 additions & 0 deletions dio/test/interceptor_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,18 @@ void main() {
expect(response.requestOptions.contentType, 'application/json');
});

test('sets application/json for List<Map> 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(
Expand Down

0 comments on commit 4204004

Please sign in to comment.