DioError [DioErrorType.RESPONSE]: Http status error [405]
Asked Answered
A

7

7

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

Afoul answered 29/12, 2019 at 17:15 Comment(4)
I have solved this question already, it was a silly mistakeAfoul
How did you solved the issue? Please mention so that the future readers may find it helpful. Thank you!Wideawake
@RavinderKumar what was the mistake?, i 'm getting error 500, bt in postman i'm getting response success..Sleeve
@Sleeve I forgot what solved this but maybe some value was null or response data was already in json and I was encoding and decoding it again, anyways if you facing internal server error maybe you should check that any parameter should not have any white spaces.Afoul
S
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;
}
Subtile answered 6/2, 2021 at 5:37 Comment(0)
H
2

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.

Henig answered 5/7, 2020 at 18:29 Comment(0)
C
1

Please try passing the params as JSON encoded.

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

Hope this helps!

Cleodal answered 16/1, 2020 at 19:31 Comment(2)
Are you sending the header as JSON vs Form encoded?Cleodal
@RayLi It's obvious from the code syntax posted in the OP that he is sending a Form encoded data.Wideawake
V
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.

Vyborg answered 7/10, 2020 at 1:10 Comment(1)
that works on me dude :)Lukas
C
0

Try to pass content type

final response = await Dio().post(Url,
        options: Options(contentType: 'multipart/form-data'), data: formData);
Conrad answered 12/9, 2020 at 11:23 Comment(0)
C
0

This particular problem occurs when the response you expect (in JSON) doesn't match the response you are looking forward to receiving.

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.

Clements answered 3/11, 2021 at 7:42 Comment(0)
P
0

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. for me i resolved like that

eg public DataResponseModel UpdateFormalize([FromForm] FormalizeDto dto){ //somes code }

Panocha answered 22/11, 2021 at 14:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.