Search code examples
node.jsjsonexpressfluttermulter

Unable to catch dioerror [dioerrortype.response]: http status error [statusCode]


dioerror [dioerrortype.response]: http status error [statusCode]

Here is the Headers part :

Headers("Authorization": "token", 
        "Content-Type" : "multipart/form-data", 
        "Accept" : "application/json")

Here is The Data part :

    try {
      FormData data = FormData.fromMap({
        "username": userName.toString(),
        "name": naMe.toString(),
        "birthday": birthDay.toString(),
        "about": aboutUser.toString(),
        "sender": sendUser.toString(),
        "mobile": userMobile.toString(),
        "avatar": _image != null
            ? await MultipartFile.fromFile(_image.path,
                filename: avatarName.toString())
            : Text('Invalid Avatar'),
      }); 
     }

Here is Dio request/response part :

try {
     Dio dio = Dio();
     dio.options.headers['Accept'] = "multipart/form-data";
     dio.options.headers['Authorization'] = "Bearer $token";
     dio.options.headers['Content-Type'] = "application/json";

     var response = await dio.post(url, data: data);
     final res = json.encode(response.data);
     print('$res');
     var responseCode = response.statusCode;
     print('$responseCode');
 }

Solution

  • The problem was solved by editing catch blog with

    On DioError catch (error) {
       var statusCode = error.response.statusCode;
       Print(statusCode);
    }