0

我知道这个问题可能是多余的,但我正在尝试通过 POST 请求向 Flutter 中的 Microsoft Custom Vision 发送 png 文件。这是我的代码:

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."}
4

1 回答 1

0

根据您的评论,我认为您使用了错误的端点/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);
于 2021-01-02T12:41:51.257 回答