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

25,474

Solution 1

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

Solution 2

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.

Solution 3

Please try passing the params as JSON encoded.

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

Hope this helps!

Solution 4

I had the same error, the BaseOptions was having different method name, other than POST... when i changed it back to POST it worked. Not sure if DIO package accepts using other than POST methods to call a Post method in API.

Share:
25,474
Ravinder Kumar
Author by

Ravinder Kumar

I am a mobile developer who loves to write Native Android as well as Flutter mobile applications.

Updated on July 09, 2022

Comments

  • Ravinder Kumar
    Ravinder Kumar 5 months

    I am creating a post request Using Dio, this is my FormData params,

        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,

    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

    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)

    Please help me solve this. My URL is=> https://vertoindiapay.com/pay/api/withdraw


    Although this is working fine in postman,

    enter image description here