cfug/dio

503 Status Code With the Https base URL

AnkitKh095 opened this issue · 1 comments

Package

dio

Version

5.4.3+1

Operating-System

Android, iOS

Adapter

Default Dio

Output of flutter doctor -v

[✓] Flutter (Channel stable, 3.22.1, on macOS 14.4.1 23E224 darwin-arm64, locale en-IN)
    • Flutter version 3.22.1 on channel stable at /Users/ankitkhaire/Documents/flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision a14f74ff3a (7 days ago), 2024-05-22 11:08:21 -0500
    • Engine revision 55eae6864b
    • Dart version 3.4.1
    • DevTools version 2.34.3

[!] Android toolchain - develop for Android devices (Android SDK version 34.0.0)
    • Android SDK at /Users/ankitkhaire/Library/Android/sdk
    • Platform android-34, build-tools 34.0.0
    • ANDROID_SDK_ROOT = /Users/ankitkhaire/Library/Android/sdk
    • Java binary at: /Applications/Android Studio.app/Contents/jbr/Contents/Home/bin/java
    • Java version OpenJDK Runtime Environment (build 17.0.7+0-17.0.7b1000.6-10550314)
    ✗ 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 15.3)
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 15E204a
    • CocoaPods version 1.14.3

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

[✓] Android Studio (version 2023.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 17.0.7+0-17.0.7b1000.6-10550314)

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

[✓] Connected device (4 available)
    • M2012K11AI (mobile)             • 438d83ed              • android-arm64  • Android 13 (API 33)
    • macOS (desktop)                 • macos                 • darwin-arm64   • macOS 14.4.1 23E224 darwin-arm64

[✓] Network resources
    • All expected network resources are available.

! Doctor found issues in 1 category.

Dart Version

3.4.1

Steps to Reproduce

class ApiService {
late Dio _dio;

ApiService() {
BaseOptions options = BaseOptions(
baseUrl: URL.BASE_URL,
contentType: Headers.jsonContentType,
responseType: ResponseType.plain,
maxRedirects: 2,
connectTimeout: const Duration(milliseconds: 60000),
receiveTimeout: const Duration(milliseconds: 60000),
);
_dio = Dio(options);

_dio.interceptors.add(InterceptorsWrapper(
  onRequest: (options, handler) async {
    String? token = await getAccessToken();

    if (token != null) {
      options.headers['Authorization'] = token; // Removed 'Bearer ' prefix
      options.headers['Authorization'] = token; // Removed 'Bearer ' prefix
    }
    print("REQUEST URL: ${options.uri}"); // Print request URL

    return handler.next(options);
  },
  onError: (DioException e, handler) async {
    if (e.response?.statusCode == 401 &&
        e.requestOptions.extra['refreshed'] != true) {
      try {
        String? newAccessToken = await refreshToken();

        if (newAccessToken != null) {
          e.requestOptions.headers['Authorization'] = newAccessToken;
          e.requestOptions.extra['refreshed'] =
              true; // Mark request as refreshed
          return handler.resolve(await _dio.fetch(e.requestOptions));
        }
      } catch (refreshError) {
        print("Error while refreshing token: $refreshError");
        throw e; // Rethrow original error
      }
    } else {
      print("Non-401 error occurred: ${e.response?.statusCode}");
    }
    return handler.next(e);
  },
));

}

// Method to perform GET requests
Future<Response> fetchData(String path) async {
try {
return await _dio.get(path);
} catch (e) {
print("Error while fetching data: $e");
throw e;
}
}

// Method to perform POST requests
Future<Response> postData(String path, dynamic data) async {
try {
return await _dio.post(path, data: data);
} catch (e) {
print("Error while posting data: $e");
throw e;
}
}

This is the file I am using for the APi call whenever I hit an api call it give me the same error every time
Non-401 error occurred: 503
I/flutter (12641): Error while posting data: DioException [bad response]: This exception was thrown because the response has a status code of 503 and RequestOptions.validateStatus was configured to throw for this status code.

Expected Result

it should return the response work normally.
it is working correctly with the http URL

Actual Result

Non-401 error occurred: 503
I/flutter (12641):
I/flutter (12641):
I/flutter (12641):
I/flutter (12641): <title>Application Error</title>
I/flutter (12641): <style media="screen">
I/flutter (12641): html,body,iframe {
I/flutter (12641): margin: 0;
I/flutter (12641): padding: 0;
I/flutter (12641): }
I/flutter (12641): html,body {
I/flutter (12641): height: 100%;
I/flutter (12641): overflow: hidden;
I/flutter (12641): }
I/flutter (12641): iframe {
I/flutter (12641): width: 100%;
I/flutter (12641): height: 100%;
I/flutter (12641): border: 0;
I/flutter (12641): }
I/flutter (12641): </style>
I/flutter (12641):
I/flutter (12641):
I/flutter (12641): <iframe src="//www.herokucdn.com/error-pages/application-error.html"></iframe>
I/flutter (12641):
I/flutter (12641):

503 is a server error, which is not related to the client.