2

I try to record my voice and send it to /speech method on Wit.ai. So, from my browser, I collect a blob like this and want to execute an $.ajax() request :

      recorder && recorder.exportWAV(function (blob) {
            callback(blob);
            // Ajax request here !
            var data = new FormData();
                data.append('file', blob);

                $.ajax({
                  url :  "https://api.wit.ai/speech?v=20171010",
                  headers: {
                    'X-Requested-With': 'JSONHttpRequest',
                    'Content-Type': 'audio/wav',
                    'Authorization' : 'Bearer OHROML6TAXxxxxxxxxxxxxSRYOVFCC'
                  },
                  type: 'POST',
                  data: data,
                  contentType: false,
                  processData: false,
                  success: function(data) {
                    alert(data);
                  },
                  error: function(error) {
                    alert("not so boa!"+JSON.stringify(error));
                  }
                });

            recorder.clear();
        }, (AudioFormat || "audio/wav"));

All my results are a 400 error ! Bad request ! Or "Mismatch content type".
Any help would be appreciate here.

I tried without success :

recorder && recorder.exportWAV(function (blob) {
                    callback(blob);




                    $.ajax({
                      type: 'POST',
                      headers: {
                        'Authorization' : 'Bearer OHROML6TAEDFxxxx5W2SRYOVFCC'
                      },
                      url: 'https://api.wit.ai/speech?v=20171010',
                      data: blob,
                      contentType: 'audio/wav', // set accordingly
                      processData: false,
                      success: function(data) {
                        alert(data);
                      },
                      error: function(error) {
                        alert("not so boa!"+JSON.stringify(error));
                      }
                    });

                                      // Clear the Recorder to start again !
                    recorder.clear();
                }, (AudioFormat || "audio/wav"));

I have still the same issues :
Bad request or Wit doesn"t recognize the sample as a wav audio.

Pablo DelaNoche
  • 677
  • 1
  • 9
  • 28

1 Answers1

0

In the sample code you provided, you're submitting a request to Wit using FormData. Per the MDN Web Docs:

FormData uses the same format a form would use if the encoding type were set to multipart/form-data.

But in your request, you're specifying a Content-Type of audio/wav. So you're sending one type of data (multipart/form-data), but saying you're sending a different type (audio/wav).

Per the Wit API docs for POST /speech:

Body
Put your binary data (file or stream) in the body.

To send your audio as binary data, follow this answer to "How can javascript upload a blob?", which includes an example using jQuery.

Jacob Budin
  • 9,753
  • 4
  • 32
  • 35
  • @PabloDelaNoche Move `recorder.clear()` into your `success` and `error` callbacks. Are you sure `blob` has the data you expect? – Jacob Budin Oct 17 '17 at 08:54