Skip to content

ImplyContentTypeInterceptor does not set content type to json when data is Json Array #1736

@MrKnos

Description

@MrKnos

Package

dio

Version

^5.0.2

Output of flutter doctor -v

[✓] Flutter (Channel stable, 3.7.5, on macOS 13.2.1 22D68 darwin-x64, locale en-TH)
    • Flutter version 3.7.5 on channel stable at /Users/kittipongtheothaisong/fvm/versions/3.7.5
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision c07f788888 (3 weeks ago), 2023-02-22 17:52:33 -0600
    • Engine revision 0f359063c4
    • Dart version 2.19.2
    • DevTools version 2.20.1

[!] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/kittipongtheothaisong/Library/Android/sdk
    ✗ cmdline-tools component is missing
      Run `path/to/sdkmanager --install "cmdline-tools;latest"`
      See https://developer.android.com/studio/command-line for more details.
    ✗ Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/docs/get-started/install/macos#android-setup for more details.

[✓] Xcode - develop for iOS and macOS (Xcode 14.2)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 14C18
    • CocoaPods version 1.11.3

[✓] Chrome - develop for the web
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2021.1)
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 11.0.11+0-b60-7590822)

[✓] IntelliJ IDEA Community Edition (version 2021.3.3)
    • IntelliJ at /Applications/IntelliJ IDEA CE.app
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart

[✓] VS Code (version 1.76.0)
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.58.0

[✓] Connected device (3 available)
    • SM N770F (mobile) • *********** • android-arm64  • Android 13 (API 33)
    • macOS (desktop)   • macos       • darwin-x64     • macOS 13.2.1 22D68 darwin-x64
    • Chrome (web)      • chrome      • web-javascript • Google Chrome 111.0.5563.64

[✓] HTTP Host Availability
    • All required HTTP hosts are available

Dart Version

Dart SDK version: 2.19.2 (stable) (Tue Feb 7 18:37:17 2023 +0000) on "macos_x64"

Steps to Reproduce

  1. Send request data as Json Array.
import 'package:dio/dio.dart';
import 'package:dio/io.dart';
import 'package:dio_imply_content_type_flutter/book_entity.dart';
import 'package:flutter/material.dart';

Future<void> main() async {
  await createBooks();
  runApp(const MyApp());
}

Future<void> createBooks() async {
  Dio client = Dio(BaseOptions(baseUrl: 'http://localhost:8081'));
  client = _setupProxy(client: client);

  const book1 = BookEntity(title: 'Book 1', authorName: 'John doe');
  const book2 = BookEntity(title: 'Book 2', authorName: 'Jane doe');

  await client.post('/books/create', data: [book1.toJson(), book2.toJson()]);
}

Dio _setupProxy({
  required Dio client,
}) {
  client.httpClientAdapter = IOHttpClientAdapter()
    ..onHttpClientCreate = (client) {
      return client..findProxy = (uri) => 'PROXY 192.168.20.60:9090';
    };

  return client;
}

BookEntity.dart

import 'package:json_annotation/json_annotation.dart';

part 'book_entity.g.dart';

@JsonSerializable()
class BookEntity {
  const BookEntity({
    required this.title,
    required this.authorName,
  });

  factory BookEntity.fromJson(
    Map<String, dynamic> json,
  ) =>
      _$BookEntityFromJson(json);

  Map<String, dynamic> toJson() => _$BookEntityToJson(this);

  final String title;

  @JsonKey(name: 'author_name')
  final String authorName;
}

Expected Result

ImplyContentTypeInterceptor should attach content-type json to request header when content type is Json Array.

Actual Result

No content-type header exist and get warning.

[🔔 Dio] List<Map<String, dynamic>> cannot be usedto imply a default content-type, please set a proper content-type in the request.
         #0      ImplyContentTypeInterceptor.onRequest (package:dio/src/interceptors/imply_content_type.dart:45:36)
         #1      DioMixin.fetch.requestInterceptorWrapper.<anonymous closure>.<anonymous closure> (package:dio/src/dio_mixin.dart:412:26)
         #2      new Future.<anonymous closure> (dart:async/future.dart:253:37)
         #3      Timer._createTimer.<anonymous closure> (dart:async-patch/timer_patch.dart:18:15)
         #4      _Timer._runTimers (dart:isolate-patch/timer_impl.dart:398:19)
         #5      _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:429:5)
         #6      _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:192:26)

Proxy log (content-type not exist)
Screenshot 2566-03-14 at 13 02 35

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixedp: dioTargeting `dio` packages: featureThis issue indicates a feature request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions