You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

Flutter POST JSON数据至API未被接收,后端PHP测试正常求解决方案

解决方案:Flutter POST JSON 到 API 失败的排查步骤

咱们先聚焦你遇到的问题:PHP脚本测试API完全正常,但Flutter应用提交JSON数据时API接收不到——虽然你已经设置了Content-Type: application/json。我来帮你一步步排查和修复:

1. 先修复代码里的明显语法错误

你的Flutter代码里有个未定义变量的问题,这可能导致异常逻辑混乱,先把它改掉:

// 原代码里的 if( json == null) 是错误的,改为:
if(response.body == null || response.body.isEmpty){
  throw Exception("Error while creating new account");
}

2. 确认生成的JSON和测试用例完全一致

先打印你编码后的JSON字符串,和PHP测试时的--data-raw内容逐字对比,确保键名、格式、数据完全匹配。比如检查createUser.toMap()是否生成了正确的键(比如"username"而不是"userName"),有没有遗漏字段。

在Flutter里添加打印代码:

var body = json.encode(createUser.toMap());
print("Generated JSON: $body"); // 把这个输出和PHP的raw data对比

3. 调整请求的编码与Headers设置

有时候Flutter的http.post直接传字符串可能会有编码问题,建议显式用UTF-8编码,并完善Headers配置:

首先导入dart:convert

import 'dart:convert';

然后修改请求代码:

static Future<String> createNewUser(String url, {required String body}) async{
  Map<String,String> headers = { 
    "Content-Type": "application/json; charset=utf-8",
  };
  try {
    final response = await http.post(
      Uri.parse(url), // 推荐用Uri.parse处理URL,避免字符串解析异常
      body: utf8.encode(body),
      headers: headers,
    );
    print("Status Code: ${response.statusCode}");
    print("Response Body: ${response.body}");
    if(response.body.isEmpty){
      throw Exception("Error while creating new account");
    }
    return response.body;
  } catch(e) {
    print("Request Error: $e");
    rethrow;
  }
}

这里有两个关键优化点:

  • Uri.parse(url)替代直接传字符串URL,避免URL格式解析问题
  • utf8.encode(body)确保JSON字符串以UTF-8编码发送,和PHP测试的编码完全一致

4. 排查服务器端的潜在限制

如果上面的步骤都没问题,那可能是服务器端对Flutter请求有特殊限制:

  • 检查服务器是否开启了CORS(跨域资源共享),如果API和Flutter应用不在同一域名下,需要服务器配置允许Flutter的请求源
  • 抓包查看Flutter实际发送的请求头(比如用Charles或Fiddler),确认Content-Type确实是application/json,没有被代理、防火墙等中间件修改

5. 备选写法:直接传Map类型的body

如果你不想手动编码JSON,也可以直接传Map给http.post,这种方式有时候更可靠:

static Future<String> createNewUser(String url, {required Map<String, dynamic> body}) async{
  Map<String,String> headers = { 
    "Content-Type": "application/json; charset=utf-8",
  };
  final response = await http.post(
    Uri.parse(url),
    body: json.encode(body),
    headers: headers,
  );
  // 后续处理逻辑和之前一致
}

调用时直接传入createUser.toMap()即可:

CreateUser.createNewUser(Config.URL_CREATE_NEW_USER, body: createUser.toMap()).then((res){
  print(res);
});

按照这些步骤排查,应该能解决你的问题。

内容的提问来源于stack exchange,提问作者Mahmoud AbdelAziz

火山引擎 最新活动