1

I have to send an object from the request. Although it works on Swagger, I cannot send it from my flutter code. When I was trying it, it always gave me Http status error 400 This problem is coming my wrong request. I examined the code of the Internet. I thought I implemented it correctly but It still does not work. Please, could you help me to solve it? Here is my interested code areas:

my api side =>

Future changePassword(id, password) async {
  try {
    var prefs = await SharedPreferences.getInstance();
    // User sendingData = User(
    //     id: id,
    // );
    // final dataSend = sendingData.toJson();
    final response = await dio.post('http://192.168.1.108:2308/api/Account/ChangePassword',
        data: jsonEncode({
          "id": "$id",
          "password": "$password"
        }),
        options: Options(headers: {
          HttpHeaders.acceptHeader: '*/*',
          HttpHeaders.contentTypeHeader: 'application/json',
        }));
    if (response.statusCode != 200) {
      // debugPrint('burası-------------------------' + response.statusCode.toString());
    }
    prefs.remove('password');
    return response;
  }
  catch (e) {
    debugPrint(e.toString());
  }
}

this is my screen side that I called it =>

onPressed: () async {
  var prefs = await SharedPreferences.getInstance();
  var pwd = prefs.getString('password');
  var email = prefs.getString('email');
  final usrId = prefs.getInt('userId');
  pwdFormKey.currentState!.save();
  if (pwdFormKey.currentState!
      .validate()) {
    try {
      if(oldPwdController.text==pwd){
        if(newPwdController.text==newPwdController2.text) {
          await api.changePassword(usrId, newPwdController.text);
              Navigator.pop(context);
              Fluttertoast.showToast(
                  msg: "Şifreniz başarıyla değiştirilmiştir.",
                  toastLength: Toast
                      .LENGTH_SHORT,
                  gravity: ToastGravity
                      .BOTTOM,
                  backgroundColor: CupertinoColors
                      .activeGreen,
                  textColor: CupertinoColors
                      .black,
                  fontSize: 16.0);
            }
          // }
        // }
           else {
          Fluttertoast.showToast(
              msg: "Yeni girdiğiniz şifreler aynı değil, lütfen kontrol ediniz.",
              toastLength: Toast.LENGTH_SHORT,
              gravity: ToastGravity.BOTTOM,
              backgroundColor: CupertinoColors.systemRed,
              textColor: CupertinoColors.black,
              fontSize: 16.0);
        }
      }
      else{
        Fluttertoast.showToast(
            msg: "Eski Şifreniz Hatalıdır.",
            toastLength: Toast.LENGTH_SHORT,
            gravity: ToastGravity.BOTTOM,
            backgroundColor: CupertinoColors.systemRed,
            textColor: CupertinoColors.black,
            fontSize: 16.0);
      }
    } catch (e) {
      return showDialog(
          context: context,
          builder: (BuildContext
          context) {
            return AlertDialog(
              title: Text(
                  "Hatalı Deneme"),
              content:
              SingleChildScrollView(
                child: ListBody(
                  children: [
                    Text(
                        "Hatalı şifre değiştirme işleminde bulundunuz. Lütfen tekrar deneyiniz.")
                  ],
                ),
              ),
              actions: [
                TextButton(
                  child: const Text(
                      'Tamam'),
                  onPressed: () {
                    Navigator.of(
                        context)
                        .pop();
                  },
                ),
              ],
            );
          });
    }
  }
},

1
  • Can you get a request to work with Postman? If so, update the question with a screenshot of the working Postman request. Commented Dec 20, 2022 at 1:12

1 Answer 1

0

In post method there are 2 diff values. One is data and another one is queryParameters. You can try in this way.

Map<String, dynamic>? param = {"userName": "name"}; 

return await (_dio.post(
        "http://192.168.1.108:2308/api/Account/ChangePassword",
        data: data, //Optional
        queryParameters: params,
      )).catchError((e) {
        if (!checkSessionExpire(e, context)) {
          throw e;
        }
      });
1
  • this does not work my friend Commented Dec 20, 2022 at 12:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.