Flutter:如何将POST图像http发送到Microsoft Cognitive Services



我正在尝试将从相机拍摄的图像POST到Microsoft Cognitive Service的Face API(使用Face-Detect方法(。然而,当我尝试时,它会返回"响应415":

{
"error": {
"code": "BadArgument",
"message": "Invalid Media Type."
}
}

这是我的这种方法的代码:

final bytes = image.readAsBytesSync();
var uri = Uri.parse("https://australiaeast.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false");
var request = new http.MultipartRequest("POST", uri);
var multipartFile = new http.MultipartFile.fromBytes('url', bytes, contentType: new MediaType('image', 'jpeg'));
request.headers['Ocp-Apim-Subscription-Key'] = "9c261636281d42c0947d89fe3982df73";
request.headers['Content-Type'] = "application/octet-stream";
request.files.add(multipartFile);
var response = await request.send();
print(request);
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
}

我使用Flutter Image Picker插件来拍摄照片,并使其在屏幕上显示得很好。我在Microsoft Cognitive Services上尝试过的所有其他操作都很好——只是上传这张图片给我带来了问题。

我认为您不需要MultipartRequest,只需要Request,并分配bodyBytes属性:

final bytes = image.readAsBytesSync();
var uri = Uri.parse("https://australiaeast.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceLandmarks=false");
var request = new http.Request("POST", uri)
..headers['Ocp-Apim-Subscription-Key'] = "9c261636281d42c0947d89fe3982df73"
..headers['Content-Type'] = "application/octet-stream"
..bodyBytes = bytes;
var response = await request.send();
print(request);
print(response.statusCode);
response.stream.transform(utf8.decoder).listen((value) {
print(value);
});

相关内容

  • 没有找到相关文章

最新更新