DioError [DioErrorType.RESPONSE]:Http状态错误[405]

时间:2019-12-29 17:15:36

标签: 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状态错误[405]] => PATH:https://vertoindiapay.com/pay/api/withdraw

     

E / flutter(6703):[错误:flutter / lib / ui / ui_dart_state.cc(157)]未处理的异常: DioError [DioErrorType.RESPONSE]:Http状态错误[405] < / p>      

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

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


尽管这在邮递员中工作正常,

enter image description here

5 个答案:

答案 0 :(得分:3)

  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;
}

答案 1 :(得分:1)

我遇到了相同的错误, BaseOptions 具有不同的app:tabIndicatorGravity="top" 名称,而不是method ...当我将其更改回POST时,工作了。不确定DIO软件包是否接受使用POST以外的方法来调用API中的Post方法。

答案 2 :(得分:0)

请尝试将参数传递为JSON编码。

(/.*)

希望这会有所帮助!

答案 3 :(得分:0)

尝试传递内容类型

final response = await Dio().post(Url,
        options: Options(contentType: 'multipart/form-data'), data: formData);

答案 4 :(得分:0)

所以我遇到了这个问题。因此,我发现您在Postman中使用的标题应该与在Dio中使用的标题匹配。例如

$('.acc_table').on('click', '.edit_account', function (e){

我的邮递员看起来像这样Postman

显然,Dio在标题方面也表现得像邮递员,因此如果邮递员的标题不匹配,显然会引发错误。

简单地说,Dio会像邮递员一样自行推断内容类型。