【问题标题】:Flutter: How to http POST image to Microsoft Cognitive ServicesFlutter:如何将 POST 图像 http 发送到 Microsoft 认知服务
【发布时间】:2019-04-08 15:24:38
【问题描述】:

我正在尝试将相机拍摄的图像发布到 Microsoft Cognitive Service 的人脸 API(使用 Face - Detect method)。但是,当我尝试它返回“响应 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 认知服务中尝试过的所有其他操作都可以正常工作 - 只是上传这张图片给我带来了问题。

【问题讨论】:

    标签: http dart flutter microsoft-cognitive


    【解决方案1】:

    我认为您不需要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);
    });
    

    【讨论】:

      猜你喜欢
      • 2017-08-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 1970-01-01
      相关资源
      最近更新 更多