简体   繁体   English

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

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

I am creating a post request Using Dio , this is my FormData params,我正在使用Dio创建一个post请求,这是我的FormData参数,

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

then I am passing params like this,然后我像这样传递params

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

But I am getting an error on request,但我收到请求时出错,

ERROR[DioError [DioErrorType.RESPONSE]: Http status error [405]] => PATH: https://vertoindiapay.com/pay/api/withdraw错误[DioError [DioErrorType.RESPONSE]:Http 状态错误[405]] => 路径: 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): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] 未处理的异常: DioError [DioErrorType.RESPONSE]:Http 状态错误 [405]

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

Please help me solve this.请帮我解决这个问题。 My URL is=> https://vertoindiapay.com/pay/api/withdraw我的网址是=> https://vertoindiapay.com/pay/api/withdraw


Although this is working fine in postman,虽然这在邮递员中工作正常,

在此处输入图片说明

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

I had the same error, the BaseOptions was having different method name, other than POST ... when i changed it back to POST it worked.我有同样的错误, BaseOptions有不同的method名称,除了POST ...当我把它改回POST它工作了。 Not sure if DIO package accepts using other than POST methods to call a Post method in API.不确定 DIO 包是否接受使用POST方法以外的方法来调用 API 中的 Post 方法。

Please try passing the params as JSON encoded.请尝试将参数作为 JSON 编码传递。

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

Hope this helps!希望这可以帮助!

Try to pass content type尝试传递内容类型

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

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.所以我发现你在 Postman 中使用的标头应该与你在 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.显然,当涉及到邮件头时,Dio 的行为也很像邮递员,因此很明显,如果来自邮递员的邮件头不匹配,那么它就会抛出错误。

Well in plain terms Dio would infer the content-type by itself just like postman would do.简单地说,Dio 会像邮递员一样自行推断内容类型。

This particular problem occurs when the response you expect (in JSON) doesn't match the response you are looking forward to receiving.当您期望的响应(以 JSON 格式)与您期待接收的响应不匹配时,就会出现此特定问题。

if this is your code,如果这是你的代码

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

Check the Response model if it matches with the JSON it receives in the Postman response.检查响应模型是否与它在 Postman 响应中收到的 JSON 匹配。

i had the same error the problem come from your server.我有同样的错误问题来自您的服务器。 your action to the server may be get datas [FromBody] use [FromForm] it will work.您对服务器的操作可能是获取数据 [FromBody] 使用 [FromForm] 它将起作用。 for me i resolved like that对我来说,我是这样解决的

eg public DataResponseModel UpdateFormalize([FromForm] FormalizeDto dto){ //somes code }例如 public DataResponseModel UpdateFormalize([FromForm] FormalizeDto dto){ //一些代码 }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 DioError [DioErrorType.RESPONSE]:Http 状态错误 [404] - DioError [DioErrorType.RESPONSE]: Http status error [404] 我收到 DioError [DioErrorType.RESPONSE]: Http status error [400] 使用 dio 和 post 方法发送表单数据请求 - I got DioError [DioErrorType.RESPONSE]: Http status error [400] when send form data using dio and post method to request Dio 错误:Http 状态错误代码 [405] Flutter - Dio Error : Http Status Error Code [ 405 ] Flutter 使用asp.net Web API和Angular 2对预检的响应具有无效的HTTP状态代码405 - Response for preflight has invalid HTTP status code 405 with asp.net web api and Angular 2 IONIC 1 XMLHttpRequest无法加载预检响应具有无效的HTTP状态代码405 - IONIC 1 XMLHttpRequest cannot load Response for preflight has invalid HTTP status code 405 角度4 Web服务405状态响应 - angular 4 web service 405 status response Flask Python 中的 HTTP 状态 405 和 Restful API - HTTP Status 405 and Restful API in Flask Python 服务器返回 HTTP 响应代码:URL 405 - Server returned HTTP response code: 405 for URL 重定向发布方法HTTP-&gt; HTTPS-HTTP状态405(春季启动) - Redirect Post method HTTP -> HTTPS - HTTP Status 405 (Spring boot) Apache Tomcat HTTP状态405-此URL不支持HTTP方法GET - Apache Tomcat HTTP Status 405 - HTTP method GET is not supported by this URL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM