retrofit.dart icon indicating copy to clipboard operation
retrofit.dart copied to clipboard

Can't use both body() and Part() in the same api call

Open devmuaz opened this issue 3 years ago • 5 comments
trafficstars

Describe the bug Hi guys, I've been trying to send a request with form data fields that contains an image file.. I ended up with incorrect dart generated file.

Reproduce Steps to reproduce the behavior:

1- Have this model that contains several properties: Screen Shot 2022-03-16 at 10 50 49 PM

2- The abstracted retrofit method: Screen Shot 2022-03-16 at 10 51 48 PM

3- The current generated file: Screen Shot 2022-03-16 at 10 52 05 PM

Expected behavior It should create a FormData variable in which we can add the model properties and the image file too

Screenshots Screen Shot 2022-03-16 at 10 53 42 PM

Retrofit and Dio Versions

dependencies:
  dio: ^4.0.4
  retrofit: ^3.0.1

dev_dependencies:
  build_runner: ^2.0.0
  retrofit_generator: ^3.0.1+1

Flutter Versions

[✓] Flutter (Channel stable, 2.10.2, on macOS 12.0.1 21A559 darwin-x64, locale en-IQ)
    • Flutter version 2.10.2 at /Users/apple/Library/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision 097d3313d8 (4 weeks ago), 2022-02-18 19:33:08 -0600
    • Engine revision a83ed0e5e3
    • Dart version 2.16.1
    • DevTools version 2.9.2

[✓] Android toolchain - develop for Android devices (Android SDK version 31.0.0)
    • Android SDK at /Users/apple/Library/Android/sdk
    • Platform android-31, build-tools 31.0.0
    • ANDROID_HOME = /Users/apple/.android
    • ANDROID_SDK_ROOT = /Users/apple/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jre/jdk/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b3-6915495)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 13.1)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • CocoaPods version 1.11.2

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

[✓] Android Studio (version 4.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 1.8.0_242-release-1644-b3-6915495)

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

[✓] Connected device (3 available)
    • iPad Pro (12.9-inch) (5th generation) (mobile) • E0AC40C8-C54E-4DA5-B674-462FA8047FEC • ios            • com.apple.CoreSimulator.SimRuntime.iOS-15-0 (simulator)
    • macOS (desktop)                                • macos                                • darwin-x64     • macOS 12.0.1 21A559 darwin-x64
    • Chrome (web)                                   • chrome                               • web-javascript • Google Chrome 99.0.4844.51

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

• No issues found!

Desktop (please complete the following information):

  • OS: MacOs
  • Browser: Chrome
  • Version: 12

Smartphone (please complete the following information):

  • Device: iPad Pro 12.9 inch
  • OS: iOS
  • Browser Chrome
  • Version 15.4

devmuaz avatar Mar 16 '22 20:03 devmuaz

@devmuaz Any luck? I am also facing the same issue.

krishnaSoni2113 avatar Apr 01 '22 11:04 krishnaSoni2113

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 01 '22 15:05 stale[bot]

+1

MaximilianFlechtner avatar May 12 '22 06:05 MaximilianFlechtner

Do it like this:

@Multipart() @POST("/doctos/update") Future<HttpResponse> updateDoctorInfo({ UpdateDoctorRequest request, File? file })

It'll work

jishnuraj-biz avatar Jul 31 '22 07:07 jishnuraj-biz

Is this solved?

wolru avatar Sep 05 '22 15:09 wolru

any updates?

masreplay avatar Dec 04 '22 16:12 masreplay

@devmuaz

masreplay avatar Dec 04 '22 19:12 masreplay

It's by design. Body is used to submit plain text data like JSON.

Please use each filed in UpdateDoctorRequest with an @Part annotation here.

trevorwang avatar Dec 05 '22 03:12 trevorwang

Hello @trevorwang it doesn't work image

it should generate something like this

final _data = FormData.fromMap(
  body.toJson(),
);

masreplay avatar Dec 05 '22 06:12 masreplay

It should be each field with a @part annotation .

{
@part String id,
@part File file,
@part String name,
}

feel free to reopen it if issue still exists.

trevorwang avatar Oct 12 '23 14:10 trevorwang