简体   繁体   English

DioError [DioErrorType.RESPONSE]: Http 状态错误 [400] 异常

[英]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.我正在开发一个 Flutter Restful web 应用程序和 web api 后端作为 asp.net 核心。 When i try to send the form data using post request it is throwing this error当我尝试使用 post 请求发送表单数据时,它抛出此错误

DioError [DioErrorType.RESPONSE]: Http status error [400] Exception DioError [DioErrorType.RESPONSE]: Http 状态错误 [400] 异常

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.但是当我在不使用 textcontroller Text 的情况下手动发送数据时,它可以工作。 Please help me to fix this请帮我解决这个问题

Working perfectly in POSTMAN在 POSTMAN 中完美运行吨

Late answer, may help you.迟到的答案,可能对你有帮助。

I was getting same error with Dio and form-data .我在使用Dioform-data时遇到了同样的错误。 It worked!有效! after adding contentType添加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()}");
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM