试试这个。如果你还没有的话,我建议你自己创建一个简单的 Dart 项目。这样您就可以在不需要手机模拟器等的情况下进行测试。
main() async {
http.MultipartRequest request =
new http.MultipartRequest('POST', Uri.parse(url));
request.headers['Prediction-Key'] = '3f4a......'; // todo - insert real value
request.files.add(
new http.MultipartFile.fromBytes(
'image',
bytes,
filename: 'somefile', // optional
contentType: new MediaType('image', 'jpeg'),
),
);
http.StreamedResponse r = await request.send();
print(r.statusCode);
}
如果文件在磁盘上,而不是在内存中,则改用fromPath 命名构造函数。尝试不同的媒体类型。我用过image/jpeg,但你可以试试application/octet-stream。
顺便说一句,在您的第一个屏幕截图中,您显示了一个内容类型,但 Postman 忽略了这一点,因为整体内容类型被 multipart-form 覆盖。在 Postman 中取消选中该行以证明这一点。
最近在 SO 上还有另一个问题,服务器错误地期望标头区分大小写。在 postman 中,再次尝试使用小写的 prediction-key 以证明服务器不介意小写的标头(这是 Dart 使用的)。