简体   繁体   中英

How to send a png file by 'application/octet-stream' in Dart/Flutter to Microsoft Custom Vision?

I know this question could be redundant, but I am trying to send a png file through a POST request to Microsoft Custom Vision in Flutter. This is my code:

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);
  }

And when I run this code I get this response:

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

Based on your comment , I think you used the wrong endpoint/URL. Since you're sending image, you have to use the other prediction endpoint that looks like this:

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

^ Note../image

If still can't, please try below code snipper(works for me):

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM