【问题标题】:How to send a png file by 'application/octet-stream' in Dart/Flutter to Microsoft Custom Vision?如何通过 Dart/Flutter 中的“application/octet-stream”将 png 文件发送到 Microsoft Custom Vision?
【发布时间】:2020-10-16 12:03:10
【问题描述】:

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

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

【问题讨论】:

标签: flutter http-post http-status-code-415 microsoft-custom-vision


【解决方案1】:

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

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

^ 注意../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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-12
    • 2017-09-02
    • 2014-06-08
    • 2016-03-13
    • 2011-01-26
    • 2019-06-26
    相关资源
    最近更新 更多