I am developing a Flutter Restful web application and the web api backend as asp.net core. When i try to send the form data using post request it is throwing this error

DioError [DioErrorType.RESPONSE]: Http status error [400] Exception

Code

onPressed: () async {
 String email_value = emailController.text;
 String password_value = passController.text;
 String fullname_value = fullnameController.text;
 var repassword_value = repassController.text;
 print("$email_value");
 if (password_value == repassword_value) {
 try{
 Dio dio = Dio();
 var body = jsonEncode(
  {
    'FullName': '$fullname_value',
    'Email': '$email_value',
    'Password': '$password_value'
  }
 );
 print("Body" + body);
 Response response = await dio.post("http://iamtv.chainuniverse.com/api/Accounts/Register",
  data: body,
  options: Options(
    contentType: Headers.jsonContentType,
  )
 );
 var jsonData = json.decode(response.data);
 print(jsonData);
 if (response.statusCode > 200 &&
    response.statusCode < 250) {
  print("Sucess");
  await loginAction();
  print("Registered");
 }
 else{
  print(jsonData);
 }

But when i send data manually without using textcontroller Text it works. Please help me to fix this

Working perfectly in POSTMAN t


Solution 1: Kishore Jethava

Late answer, may help you.

I was getting same error with Dio and form-data. It worked! after adding contentType

FormData formData = FormData.fromMap({
  "image-param-name": await MultipartFile.fromFile(
    imageFile.path,
    filename: fileName,
    contentType: new MediaType("image", "jpeg"), //add this
  ),
});

complete code

var dio = Dio();
String fileName = imageFile.path.split('/').last;
FormData formData = FormData.fromMap({
  "image-param-name": await MultipartFile.fromFile(
    imageFile.path,
    filename: fileName,
    contentType: new MediaType("image", "jpeg"), //add this
  ),
});

var response = await dio.post(
  "url",
  data: formData,
  options: Options(
    headers: {
      "Authorization": auth-token
    },
  ),
  onSendProgress: (int sent, int total) {
    debugPrint("sent${sent.toString()}" + " total${total.toString()}");
  },
).whenComplete(() {
  debugPrint("complete:");
}).catchError((onError) {
  debugPrint("error:${onError.toString()}");
});