http - DioError [DioErrorType.RESPONSE] : Http status error [405] [Solved]

标签 http flutter flutter-dependencies dio

我正在使用 Dio 创建一个 post 请求,
这是我的 FormData 参数,

    FormData formData = FormData.fromMap({
      'wallet_id': '${dropdownValue.walletId}',
      'member_id': '${_loginModel.memberId}',
      'draw_amount': withdrawalAmountContoller.text,
      'login_password': passwordController.text,
    });

然后我像这样传递 params
Response response = await dio.post(url, data: params);
但我收到请求时出错,

ERROR[DioError [DioErrorType.RESPONSE]: Http status error [405]] => PATH: https://vertoindiapay.com/pay/api/withdraw

E/flutter ( 6703): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: DioError [DioErrorType.RESPONSE]: Http status error [405]

E/flutter ( 6703): #0 DioMixin._request._errorInterceptorWrapper. (package:dio/src/dio.dart:848:13)


请帮我解决这个问题。 我的网址是=> https://vertoindiapay.com/pay/api/withdraw

虽然这在 postman 中工作正常,
enter image description here

最佳答案

  Future<void> signUpUser() async {
    final formData = {
      'username': 'test1',
      'password': 'abcdefg',
      'grant_type': 'password',
    };
 try {
    Dio _dio = new Dio();
    _dio.options.contentType = Headers.formUrlEncodedContentType;

    final responseData = await _dio.post<Map<String, dynamic>>('/token',
        options: RequestOptions(
           
            method: 'POST',
            headers: <String, dynamic>{},
            baseUrl: 'http://52.66.71.229/'),
        data: formData);

   
      print(responseData.toString());
    } catch (e) {
      final errorMessage = DioExceptions.fromDioError(e).toString();
      print(errorMessage);
    }

  }



 class DioExceptions implements Exception {
  
  DioExceptions.fromDioError(DioError dioError) {
    switch (dioError.type) {
      case DioErrorType.CANCEL:
        message = "Request to API server was cancelled";
        break;
      case DioErrorType.CONNECT_TIMEOUT:
        message = "Connection timeout with API server";
        break;
      case DioErrorType.DEFAULT:
        message = "Connection to API server failed due to internet connection";
        break;
      case DioErrorType.RECEIVE_TIMEOUT:
        message = "Receive timeout in connection with API server";
        break;
      case DioErrorType.RESPONSE:
        message =
            _handleError(dioError.response.statusCode, dioError.response.data);
        break;
      case DioErrorType.SEND_TIMEOUT:
        message = "Send timeout in connection with API server";
        break;
      default:
        message = "Something went wrong";
        break;
    }
  }

  String message;

  String _handleError(int statusCode, dynamic error) {
    switch (statusCode) {
      case 400:
        return 'Bad request';
      case 404:
        return error["message"];
      case 500:
        return 'Internal server error';
      default:
        return 'Oops something went wrong';
    }
  }

  @override
  String toString() => message;
}

关于http - DioError [DioErrorType.RESPONSE] : Http status error [405] [Solved],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59521594/

相关文章:

flutter - 我可以在Flutter Bottom导航栏中的图标周围添加间距吗?

firebase - flutter :FlutterFcmService - 致命:找不到回调

android - Flutter 不提示设备进行授权对话框

Chewie_player 和本地化上的 flutter 构建错误

Delphi indy 流式 Http 服务器

macos - SocketException : Connection failed (OS Error: Operation not permitted, errno = 1) 在 macOS 上使用 flutter 应用程序

http - Linux 上简单快速的工具,用于观察即将到来的 http 请求

angular - 如何修复 DOMException : "The URI is malformed." in Angular 6 running HTTP?

flutter - 如何在折线图中实现 API 数据

flutter - 我如何在本地存储中存储变量或说列表变量?