Search code examples
flutterrestazure-cognitive-servicesflutter-http

How to sent image in 'application/octet-stream' content type to Azure Cognitive Services in flutter


Hi I'am trying to sent a local image using application/octet-stream' content type to Azure Cognitive Services from flutter. I have follow the following but it seem to be missing the example. The only example is for 'application/json'

Future getAnalisis() async {
    final uri =
        "https://****.cognitiveservices.azure.com/vision/v3.1/analyze?visualFeatures=Categories,Description&details=Landmarks";
    final respon = await http.post(
      uri,
      body: _file,
      headers: {
        'Ocp-Apim-Subscription-Key': '*****',
        'Content-Type': 'application/octet-stream'
      },
    );
    _imageVision = serializers.deserializeWith(
        ImageVision.serializer, jsonDecode(respon.body));
    notifyListeners();
  }

and the _file is from

final pickedFile = await picker.getImage(source: ImageSource.camera);
if (pickedFile != null) _file = File(pickedFile.path);

I also have tried the following but it of course error

   body: jsonDecode({'url' : _file.path}),
      headers: {
        'Ocp-Apim-Subscription-Key': '*****',
        'Content-Type': 'application/json'
      },

and currently my error message is

 Invalid request body "File:
'/storage/emulated/0/Android/data/com.example.testRun/files/Pictures/7702e794-3a57-4a3a-a271-fd7c0964544d
4388330820207050041.jpg'".

Solution

  • body: _file.readAsBytesSync(),