6

我正在使用Diopost创建一个请求,这是我的参数,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);

但我收到一个错误请求,

错误[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]

E/颤振(6703):#0 DioMixin._request._errorInterceptorWrapper。(包:dio/src/dio.dart:848:13)

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


虽然这在邮递员中工作正常,

在此处输入图像描述

4

7 回答 7

6
  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;
}
于 2021-02-06T05:37:35.313 回答
1

So I had this issue. So I found out that the headers you use in Postman should match the headers you are using in Dio. Like for example

  headers: {
      'Accept': "application/json",
      'Authorization': 'Bearer $token',
    },

and my Postman looks like this Postman

Apparently Dio behaves like postman when it comes to headers too so apparently if the headers from postman mis-match then it will throw an error.

Well in plain terms Dio would infer the content-type by itself just like postman would do.

于 2020-10-07T01:10:08.407 回答
1

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

Response response = await dio.post(url, data: json.encode(params));

希望这可以帮助!

于 2020-01-16T19:31:10.883 回答
1

我有同样的错误,BaseOptions有不同的method名称,除了POST......当我把它改回POST它工作时。不确定 DIO 包是否接受使用其他POST方法在 API 中调用 Post 方法。

于 2020-07-05T18:29:15.817 回答
0

当您期望的响应(以 JSON 格式)与您期望接收的响应不匹配时,就会出现此特定问题。

如果这是你的代码,

Response response = await dio.post(url, data: params);

检查 Response 模型是否与它在 Postman 响应中收到的 JSON 匹配。

于 2021-11-03T07:42:12.210 回答
0

尝试传递内容类型

final response = await Dio().post(Url,
        options: Options(contentType: 'multipart/form-data'), data: formData);
于 2020-09-12T11:23:34.893 回答
0

我遇到了同样的错误,问题来自您的服务器。您对服务器的操作可能是获取数据 [FromBody] 使用 [FromForm] 它将起作用。

对我来说,我是这样解决的:

eg public DataResponseModel<UserDto> UpdateFormalize([FromForm] FormalizeDto dto){
//somes code
}
于 2021-11-22T14:10:06.003 回答