Flutter/Dart POST请求因text/plain媒体类型报错的解决方法
解决Flutter POST请求媒体类型不支持问题
问题根源
服务器仅接受application/json格式的请求体,而你的请求默认使用了text/plain类型,因此被拒绝。
修复方案
直接在请求头里指定Content-Type为application/json,同时确保请求体是合法的JSON字符串(你的代码已经用jsonEncode处理过,这部分没问题)。
修改后的完整代码:
Future<String> sendRequest() async { final jsonArray = json.encode(body); algo.Encrypted encrypted = Algorithm.encrypt(algorithmKey, jsonArray); final encryptedBody = jsonEncode({'body': encrypted.base64}); final response = await http.post( Uri.parse("$baseUrl/$paymentMethod"), headers: { "Key": key, "secretKey": secretKey, "Content-Type": "application/json", // 新增该行,指定请求体类型 }, body: encryptedBody, ); return response.body; }
验证要点
- 对照Postman成功请求的Headers,确认
Content-Type字段是否一致; - 可以打印
encryptedBody的值,检查是否为标准JSON格式(如{"body": "xxx"})。
内容的提问来源于stack exchange,提问作者Munsif Ali
相关产品推荐
相关产品推荐

