Raj
Raj

Reputation: 121

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

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

Upvotes: 6

Views: 38807

Answers (3)

Kisakyamukama
Kisakyamukama

Reputation: 109

Was facing the same issue though from my end it was about the format I was

Check your format to ensure it aligns with this

 FormData formData = FormData.fromMap({
  'image': MultipartFile.fromBytes(
    imageBytes,
    filename: 'image.jpg',
    contentType: MediaType('image', 'jpeg'), // Adjust content type if necessary
  ),
  'key1': 'value1', // Additional data
  'key2': 'value2', // Additional data
});

Upvotes: 0

Suman Sahoo
Suman Sahoo

Reputation: 41

In this answer we have to import http parser.

import 'package:http_parser/http_parser.dart';

Upvotes: 0

Kishore Jethava
Kishore Jethava

Reputation: 6834

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()}");
});

Upvotes: 10

Related Questions