如何在Dart/Flutter中通过'application/octet-stream'将png文件发送到自定义视觉Microsoft?



我知道这个问题可能是多余的,但我正在尝试通过 POST 请求发送一个 png 文件以在 Flutter 中Microsoft自定义视觉。 这是我的代码:

void _makeRequest (File file) async {
String url = "<url>";
Map<String, String> headers = {
"Content-Type": "application/octet-stream",
"Prediction-Key": "<key>",
};         
var bytes = file.readAsBytesSync();

var response = await http.post(
url,
headers: headers,
body: bytes,
);
print(response.body);
print(response.statusCode);
}

当我运行此代码时,我得到以下响应:

{"code":"ErrorUnknown","message":"The request entity's media type 'appliction/octet-stream' is not supported for this resource."}

根据您的评论,我认为您使用了错误的端点/URL。由于要发送图像,因此必须使用如下所示的其他预测终结点:

"https://southcentralus.api.cognitive.microsoft.com/customvision/v3.0/Prediction/<Project ID>/classify/iterations/<Iteration number>/image

^注意。。/图像

如果仍然不能,请尝试以下代码狙击手(对我有用(:

final bytes = file.readAsBytesSync();
var uri = Uri.parse(
"<Prediction endpoint>");
var request = new http.Request("POST", uri)
..headers['Prediction-Key'] = "<Prediction Key>"
..headers['Content-Type'] = "application/octet-stream"
..bodyBytes = bytes;
http.Response response = await http.Response.fromStream(await request.send());
print(request);
print("Result: ${response.statusCode}");
print(response.statusCode);
print(response.body);

最新更新