Flutter调用用户创建API成功却返回500错误,请求帮助
Flutter调用注册API返回500但用户已创建的问题排查
我是Dio新手,在Flutter中调用API创建新用户时遇到问题:调用createUser()函数后,后端已成功创建用户,但Dio抛出状态码为500的DioError。切换到http库调用,仍出现相同的500错误。
一、Dio实现代码
Future<User> createUser({ required String email, required String username, required String password, required String password2, }) async { BaseOptions options = BaseOptions( baseUrl: 'https://XXXXXX.XXXX.com', ); final Dio dio = Dio(options); Response? response; try { response = await dio.post( '/signup_person', data: { 'email': email, 'username': username, 'password': password, 'password2': password2 }, ); print('Request completed'); } on DioError catch (e) { if (e.response != null) { // This is the block that gets executed print('kmfovrnf'); print(e.response!.data); print('====='); print(e.error); print('====='); print(e.response!.headers); print('====='); print(e.response!.requestOptions); } else { print('vinro'); // Something happened in setting up or sending the request that triggered an Error print(e.requestOptions); print(e.message); } rethrow; } catch (e) { print('Network Helper catching error: $e'); throw Exception(e); } print('Here'); if (response.statusCode == 201) { final Map<String, dynamic> responseMap = response.data; print('Here22'); return User.fromJson(responseMap); } else { print('Response : ${response.data}'); throw Exception(response.data.toString()); } }
二、Dio调用终端日志
Performing hot restart... Restarted application in 6,075ms. I/flutter (28797): kmfovrnf I/flutter (28797): <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="robots" content="NONE,NOARCHIVE"> <title>DoesNotExist at /signup_person</title> <style type="text/css"> html * { padding:0; margin:0; } body * { padding:10px 20px; } body * * { padding:0; } body { font:small sans-serif; background-color:#fff; color:#000; } body>div { border-bottom:1px solid #ddd; } h1 { font-weight:normal; } h2 { margin-bottom:.8em; } h3 { margin:1em 0 .5em 0; } h4 { margin:0 0 .5em 0; font-weight: normal; } code, pre { font-size: 100%; white-space: pre-wrap; word-break: break-word; } summary { cursor: pointer; } table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; } tbody td, tbody th { vertical-align:top; padding:2px 3px; } thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px; border:1px solid #ddd; ===== I/flutter (28797): Http status error [500] ===== I/flutter (28797): keep-alive: timeout=5, max=100 I/flutter (28797): date: Wed, 04 Jan 2023 09:05:45 GMT I/flutter (28797): transfer-encoding: chunked I/flutter (28797): vary: Accept-Encoding I/flutter (28797): content-encoding: gzip I/flutter (28797): referrer-policy: same-origin I/flutter (28797): content-type: text/html; charset=utf-8 I/flutter (28797): x-frame-options: DENY I/flutter (28797): cross-origin-opener-policy: same-origin I/flutter (28797): x-content-type-options: nosniff I/flutter (28797): x-turbo-charged-by: LiteSpeed I/flutter (28797): server: LiteSpeed ===== I/flutter (28797): Instance of 'RequestOptions' E/flutter (28797): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Exception: Error occurred E/flutter (28797): #0 makeRequest (package:reprex/main.dart:84:7) E/flutter (28797): <asynchronous suspension> E/flutter (28797): #1 MyApp.build.<anonymous closure> (package:reprex/main.dart:25:21) E/flutter (28797): <asynchronous suspension> E/flutter (28797):
三、Postman测试结果
Postman中使用相同参数调用该接口,能正常返回201状态码。
四、切换http库后的代码及日志
http库实现代码
Uri url = Uri.https('XXXXXXX.XXXXX.com', '/signup_person'); var response = await http.post( url, body: { 'email': email, 'username': username, 'password': password, 'password2': password2 }, ); print('====================='); print('Response status: ${response.statusCode}'); print('====================='); print('Response body: ${response.body}'); print('====================='); print('Response request: ${response.request}'); print('====================='); print('Request completed');
http库调用终端日志
I/flutter (30166): Response status: 500 I/flutter (30166): ===================== I/flutter (30166): Response body: <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8"> <meta name="robots" content="NONE,NOARCHIVE"> <title>SMTPRecipientsRefused at /signup_person</title> <style type="text/css"> html * { padding:0; margin:0; } body * { padding:10px 20px; } body * * { padding:0; } body { font:small sans-serif; background-color:#fff; color:#000; } body>div { border-bottom:1px solid #ddd; } h1 { font-weight:normal; } h2 { margin-bottom:.8em; } h3 { margin:1em 0 .5em 0; } h4 { margin:0 0 .5em 0; font-weight: normal; } code, pre { font-size: 100%; white-space: pre-wrap; word-break: break-word; } summary { cursor: pointer; } table { border:1px solid #ccc; border-collapse: collapse; width:100%; background:white; } tbody td, tbody th { vertical-align:top; padding:2px 3px; } thead th { padding:1px 6px 1px 3px; background:#fefefe; text-align:left; font-weight:normal; font-size:11px I/flutter (30166): ===================== I/flutter (30166): Response request: POST https://XXXXXX.XXX/signup_person I/flutter (30166): ===================== I/flutter (30166): Request completed
排查思路
- 后端后续操作失败:日志中出现
DoesNotExist和SMTPRecipientsRefused错误,说明后端在成功创建用户后,执行后续操作(比如发送验证邮件、查找关联资源)时出错,导致返回500状态码,但用户数据已写入数据库。 - 请求差异问题:Postman请求正常但Flutter端报错,需对比两者的请求头(比如
Content-Type、User-Agent)是否一致。Dio默认发送application/json格式数据,若后端期望form-data或x-www-form-urlencoded,可能导致后续逻辑异常。 - 邮箱有效性问题:
SMTPRecipientsRefused表明SMTP服务器拒绝收件人,需检查测试用邮箱是否有效,是否被服务器列入黑名单。 - 后端日志排查:查看后端完整错误日志,确认500错误的具体触发点,区分是资源不存在还是邮件发送失败导致的问题。
内容的提问来源于stack exchange,提问作者Olumide
相关产品推荐
相关产品推荐

