cfug / dio

A powerful HTTP client for Dart and Flutter, which supports global settings, Interceptors, FormData, aborting and canceling a request, files uploading and downloading, requests timeout, custom adapters, etc.
https://dio.pub
MIT License
12.5k stars 1.51k forks source link

Any http requests are not working with 5.0.0 #1648

Closed bhanuka96 closed 1 year ago

bhanuka96 commented 1 year ago

Package

dio

Version

5.0.0

Output of flutter doctor -v

Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
    • Android SDK at /Users/bhanukaisuru/Library/Android/sdk
    • Platform android-33, build-tools 32.1.0-rc1
    • Java binary at: /Library/Java/JavaVirtualMachines/jdk-18.0.1.1.jdk/Contents/Home/bin/java
    • Java version Java(TM) SE Runtime Environment (build 18.0.1.1+2-6)
    • All Android licenses accepted.

[✓] 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 2022.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
    ✗ Unable to find bundled Java version.
    • Try updating or re-installing Android Studio.

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

[✓] Connected device (3 available)
    • PRA LX2 (mobile) • 6MU7N17A12000618 • android-arm64  • Android 8.0.0 (API 26)
    • macOS (desktop)  • macos            • darwin-arm64   • macOS 13.2 22D49 darwin-arm64
    • Chrome (web)     • chrome           • web-javascript • Google Chrome 110.0.5481.77

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

Dart Version

No response

Steps to Reproduce

  1. Upgrade to v5.0.0

any HTTP request not working with 5.0.0.

ex: POST

final response = await _dio.post('${env.cloudFunctionUrl}/createPayment', data: json, options: Options(headers: {'Authorization': 'Bearer $token'}));

API

  if (!req.body) {
    await cf.errorBooking(req.body, 'initBodyCheck')
    log.loggerError('createPayment', t0, req.body, 'Req is null')
    res.status(400).json({
      status: 'error',
      message: "Something went wrong and we couldn't complete your request. Please login again and try again."
    })
    return
  }

It triggers the null condition req.body. I now downgrade to ^4.0.6 now, works fine.

pubspec.yaml

  dependencies:
  flutter:
    sdk: flutter
  cached_network_image: ^3.2.3
  camera: ^0.10.3
  carousel_slider: ^4.2.1
  cloud_firestore: ^4.3.2
  cupertino_icons: ^1.0.5
  device_info_plus: ^8.1.0
  dio: ^4.0.6
  dotted_border: ^2.0.0+3
  extended_image: ^7.0.2
  facebook_app_events: ^0.18.3
  file_picker: ^5.2.5
  firebase_auth: ^4.2.6
  firebase_analytics: ^10.1.1
  firebase_core: ^2.5.0
  firebase_crashlytics: ^3.0.12
  firebase_database: ^10.0.10
  firebase_dynamic_links: ^5.0.12
  firebase_messaging: ^14.2.2
  firebase_storage: ^11.0.11
  flutter_google_places_hoc081098: ^1.2.0
  flutter_widget_from_html_core: ^0.10.0
  flutter_native_image: ^0.0.6+1
  flutter_slidable: ^2.0.0
  flutter_sound: ^9.2.13
  flutter_svg: ^2.0.1
  fluttertoast: ^8.1.3
  geolocator: ^9.0.2
  gesture_x_detector: ^1.1.1
  google_maps_flutter: ^2.2.3
  google_maps_webservice: ^0.0.19
  image_picker: ^0.8.6+1
  intl: ^0.17.0
  in_app_review: ^2.0.6
  visibility_detector: ^0.3.3
  just_audio: ^0.9.31
  lottie: ^2.2.0
  navigation_history_observer: ^1.1.0
  package_info_plus: ^3.0.3
  path_provider: ^2.0.12
  permission_handler: ^10.2.0
  photo_manager: ^2.5.2
  photo_view: ^0.14.0
  pin_code_text_field: ^1.8.0
  provider: ^6.0.5
  scroll_to_index: ^3.0.1
  share_plus: ^6.3.1
  shared_preferences: ^2.0.17
  shimmer: ^2.0.0
  smooth_page_indicator: ^1.0.1
  strings: ^0.2.2
  transparent_image: ^2.0.0
  tuple: ^2.0.1
  url_launcher: ^6.1.9
  video_player: ^2.5.1
  webview_flutter: ^4.0.2
  wechat_assets_picker: ^8.4.0

Expected Result

Working

Actual Result

req.body is null

simonpham commented 1 year ago

request body is not sent. Look like #1642 has the same issue.

AlexV525 commented 1 year ago

Please make sure your content type has been set properly, and provide reproducible example.

simonpham commented 1 year ago

@AlexV525 Content type is set correctly. Nothing is wrong. It was working normally in dio version 4.0.6. This issue only happens when upgrade to dio 5.0.0.

AlexV525 commented 1 year ago

We need reproducible example at least to identify what's going wrong.

cleong98 commented 1 year ago

@bhanuka96 i think set Headers.formUrlEncodedContentType in your DIO options is ok , after i set it i can get the request body data

Screenshot 2023-02-14 at 4 47 23 PM

update

if using Headers.formUrlEncodedContentType the data value will convert to string, but in Headers.jsonContentType will convert data value as valid json.

maybe can refer what-are-the-differences-between-application-json-and-application-x-www-form-url

simonpham commented 1 year ago

@cleong98 Thank you, it works after I changed to Headers.formUrlEncodedContentType as you suggested

It didn't work when I used Headers.jsonContentType. If this is not a bug, I think it should be noted in the doc or changelog since it's working normally before in version 4.0.6.

cleong98 commented 1 year ago

@simonpham is welcome, actually i also found the answer in documentation, maybe u are not noticed when you read documentation.

Screenshot 2023-02-14 at 5 19 53 PM
AlexV525 commented 1 year ago

It's addressed already in the CHANGELOG and the migration guide.

bhanuka96 commented 1 year ago

It's working after adding contentType: Headers.jsonContentType. Thanks.