Search code examples
azureflutterdartazure-speech

Getting error 400 when trying to use Azure Speech Recognition and Flutter


I've been given the task to use the Azure Speech Recognition API on a Flutter application. The app is supposed to record the user's voice and send it to the Azure API. I've tried to use the only pub.dev plugin that I could find, but it did not work and the documentation does not have a Flutter example. Since the request returned 200 on Postman and I was able to make it work on a Javascript application, the problem must be my Flutter application, maybe something on the request, since it is returning code 400 (bad request), saying that the request contains invalid data.

The code below is my request to the API. The file which I'm using to get the bytes is a wav file containing the recorded voice

Could you help me? Thanks for the attention.

      var bytes = file.readAsBytesSync();
      var response = await Dio().post(
        "https://brazilsouth.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=pt-BR",
        data: bytes,
        options: Options(
            headers: {
              "Ocp-Apim-Subscription-Key": "subscriptionKey",
              "Content-Type": "audio/wav"
            },
      );
      print(response.statusCode);

Solution

  • After trying to solve this problem for a couple of days, I finally got a successful response!

    Future<dynamic> speechToText(File file) async {
        
        final bytes = file.readAsBytesSync();
    
        var headers = {
          'Ocp-Apim-Subscription-Key': key,
          'Content-Type': 'audio/wav'
        };
    
        var response;
        Map<String, dynamic> responseBody;
        var recognizedVoiceText;
    
        try {
          response = await http.post(
            "https://brazilsouth.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=pt-BR",
            body: bytes,
            headers: headers,
          );
    
          // The response body is a string that needs to be decoded as a json in order to extract the text.
          responseBody = jsonDecode(response.body);
          recognizedVoiceText = responseBody["DisplayText"];
        } catch (e) {
          print('Error: ${e.toString()}');
          recognizedVoiceText = "Something went wrong";
        }
    
        return recognizedVoiceText;
      }