DioError [DioErrorType.RESPONSE]: Http 状态错误 [500],为什么?
DioError [DioErrorType.RESPONSE]: Http status error [500], why?
我是 flutter 的新手,我在我的项目中使用 Dio 来使会员注册功能..但是每次我执行注册过程时,调试控制台都会给我这个错误
I/flutter (13428): Instance of 'FormData'
I/flutter (13428): DioError [DioErrorType.RESPONSE]: Http status error [500]
然而,当我尝试向 POSTMAN 注册时,却成功了。所以我不确定问题出在哪里..这是我的代码,你们能帮帮我吗?我已经坚持了几个星期了..
final String url = "https://api.censored.org/api/members/register";
Dio.FormData formData = Dio.FormData.fromMap({
"nama_lengkap": name,
"tempat_lahir": tempatlahir,
"tanggal_lahir": selectedDate.toString(),
"email": email,
"password": password,
"username": username,
"nomor_ktp": noKTP,
"alamat_ktp": alamatktpmember,
"alamat_domisili": alamatmember,
"pekerjaan": pekerjaanmember,
"alamat_pekerjaan": alamatperusahaanmember,
"no_whatsapp": noWAmember,
"no_hp": noteleponmember,
"nama_pemilik_rekening": namarekmember,
"nomor_rekening": norekmember,
"bank_id": _valBank,
"aggrement": _eulargprogramming,
//Foto
"foto_ktp": await Dio.MultipartFile.fromFile(_fotoKtp.path),
"selfie_dengan_ktp": await Dio.MultipartFile.fromFile(_fotoSelfie.path),
"foto_kk": await Dio.MultipartFile.fromFile(_fotoKK.path),
"foto_pengenal_lainnya":
await Dio.MultipartFile.fromFile(_fotoKartu.path),
"foto_buku_rekening":
await Dio.MultipartFile.fromFile(_fotoRekening.path),
"foto_profil": await Dio.MultipartFile.fromFile(_fotoSelfie.path),
//Array
"penjamin": {
"stakeholder_id": _subrgProgramming1,
"nama": penjaminNama,
"nomor_ktp": penjaminNoktp,
"alamat_ktp": penjaminAlamat,
"alamat_domisili": penjaminDomisili,
"no_hp": notelppenjamin,
},
"sosial_media": [
{
"sosial_media_id": _sosmedrgprogramming,
"nama": sosialmediamember,
"screenshot": await Dio.MultipartFile.fromFile(_fotoSosmed.path),
}
],
"saudara_kerabat": [
{
"stakeholder_id": 4,
"nama": namasaudara,
"no_hp": notelpsaudara,
},
{
"stakeholder_id": 5,
"nama": namakerabat1,
"no_hp": notelpkerabat1,
},
{
"stakeholder_id": 5,
"nama": namakerabat2,
"no_hp": notelpkerabat2,
},
{
"stakeholder_id": 5,
"nama": namakerabat3,
"no_hp": notelpkerabat3,
}
],
});
print(formData);
var res;
try {
Dio.Dio doo = Dio.Dio();
doo.options.headers['Accept'] = 'application/json';
doo.options.contentType = 'application/json';
doo.options.followRedirects = false;
Dio.Response response = await doo.post(url, data: formData);
if (response.statusCode == 200) {
print(response.data);
res = response;
final data = jsonDecode(res.body);
print(res);
int regvalue = data['value'];
String message = data['message'];
if (regvalue == 1) {
setState(() {
Navigator.pop(context);
});
print(message);
registerToast(message);
} else if (regvalue == 2) {
print(message);
registerToast(message);
} else {
print(message);
registerToast(message);
}
}
} on Dio.DioError catch (e) {
if (e.response.statusCode == 422) {
print(e.response.data);
}
} on Dio.DioError catch (e) {
if (e.response.statusCode == 500) {
print(e.response.data);
}
}
}
registerToast(String toast) {
return Fluttertoast.showToast(
msg: toast,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIos: 1,
backgroundColor: Colors.red,
textColor: Colors.white);
}
这意味着你的服务器响应 InternalServerError
但 dio 认为这是一个例外来解决这个问题要么使用 try and catch blocs 要么将它传递给你的 dio instace
final res = await dio.delete(
url,
data: postData,
options: Options(
followRedirects: false,
// will not throw errors
validateStatus: (status) => true,
headers: headers,
),
);
我在尝试使用 Dio 发送图像时遇到了同样的问题。但是只有当我在 mac 中使用 iphone 模拟器时才会出现问题,在 android 模拟器中一切正常,所以我尝试开始调试并在真实 iphone,没有模拟器,我没有遇到这个错误。
也许你也有同样的问题,希望能帮到你。
我是 flutter 的新手,我在我的项目中使用 Dio 来使会员注册功能..但是每次我执行注册过程时,调试控制台都会给我这个错误
I/flutter (13428): Instance of 'FormData'
I/flutter (13428): DioError [DioErrorType.RESPONSE]: Http status error [500]
然而,当我尝试向 POSTMAN 注册时,却成功了。所以我不确定问题出在哪里..这是我的代码,你们能帮帮我吗?我已经坚持了几个星期了..
final String url = "https://api.censored.org/api/members/register";
Dio.FormData formData = Dio.FormData.fromMap({
"nama_lengkap": name,
"tempat_lahir": tempatlahir,
"tanggal_lahir": selectedDate.toString(),
"email": email,
"password": password,
"username": username,
"nomor_ktp": noKTP,
"alamat_ktp": alamatktpmember,
"alamat_domisili": alamatmember,
"pekerjaan": pekerjaanmember,
"alamat_pekerjaan": alamatperusahaanmember,
"no_whatsapp": noWAmember,
"no_hp": noteleponmember,
"nama_pemilik_rekening": namarekmember,
"nomor_rekening": norekmember,
"bank_id": _valBank,
"aggrement": _eulargprogramming,
//Foto
"foto_ktp": await Dio.MultipartFile.fromFile(_fotoKtp.path),
"selfie_dengan_ktp": await Dio.MultipartFile.fromFile(_fotoSelfie.path),
"foto_kk": await Dio.MultipartFile.fromFile(_fotoKK.path),
"foto_pengenal_lainnya":
await Dio.MultipartFile.fromFile(_fotoKartu.path),
"foto_buku_rekening":
await Dio.MultipartFile.fromFile(_fotoRekening.path),
"foto_profil": await Dio.MultipartFile.fromFile(_fotoSelfie.path),
//Array
"penjamin": {
"stakeholder_id": _subrgProgramming1,
"nama": penjaminNama,
"nomor_ktp": penjaminNoktp,
"alamat_ktp": penjaminAlamat,
"alamat_domisili": penjaminDomisili,
"no_hp": notelppenjamin,
},
"sosial_media": [
{
"sosial_media_id": _sosmedrgprogramming,
"nama": sosialmediamember,
"screenshot": await Dio.MultipartFile.fromFile(_fotoSosmed.path),
}
],
"saudara_kerabat": [
{
"stakeholder_id": 4,
"nama": namasaudara,
"no_hp": notelpsaudara,
},
{
"stakeholder_id": 5,
"nama": namakerabat1,
"no_hp": notelpkerabat1,
},
{
"stakeholder_id": 5,
"nama": namakerabat2,
"no_hp": notelpkerabat2,
},
{
"stakeholder_id": 5,
"nama": namakerabat3,
"no_hp": notelpkerabat3,
}
],
});
print(formData);
var res;
try {
Dio.Dio doo = Dio.Dio();
doo.options.headers['Accept'] = 'application/json';
doo.options.contentType = 'application/json';
doo.options.followRedirects = false;
Dio.Response response = await doo.post(url, data: formData);
if (response.statusCode == 200) {
print(response.data);
res = response;
final data = jsonDecode(res.body);
print(res);
int regvalue = data['value'];
String message = data['message'];
if (regvalue == 1) {
setState(() {
Navigator.pop(context);
});
print(message);
registerToast(message);
} else if (regvalue == 2) {
print(message);
registerToast(message);
} else {
print(message);
registerToast(message);
}
}
} on Dio.DioError catch (e) {
if (e.response.statusCode == 422) {
print(e.response.data);
}
} on Dio.DioError catch (e) {
if (e.response.statusCode == 500) {
print(e.response.data);
}
}
}
registerToast(String toast) {
return Fluttertoast.showToast(
msg: toast,
toastLength: Toast.LENGTH_SHORT,
gravity: ToastGravity.BOTTOM,
timeInSecForIos: 1,
backgroundColor: Colors.red,
textColor: Colors.white);
}
这意味着你的服务器响应 InternalServerError
但 dio 认为这是一个例外来解决这个问题要么使用 try and catch blocs 要么将它传递给你的 dio instace
final res = await dio.delete(
url,
data: postData,
options: Options(
followRedirects: false,
// will not throw errors
validateStatus: (status) => true,
headers: headers,
),
);
我在尝试使用 Dio 发送图像时遇到了同样的问题。但是只有当我在 mac 中使用 iphone 模拟器时才会出现问题,在 android 模拟器中一切正常,所以我尝试开始调试并在真实 iphone,没有模拟器,我没有遇到这个错误。 也许你也有同样的问题,希望能帮到你。