首页 > 解决方案 > api调用和邮递员之间的区别

问题描述

我正在使用 Azure Speech rest api。我在带有 .wav 文件的邮递员上进行了尝试,它成功地返回了结果。但是,当我从我的 node.js 代码中调用 api 时。即使我提供相同的音频文件,它也总是返回不支持的音频格式。谁能告诉我它们有什么区别?或者 Postman 做了什么让它发挥作用?

下面是我如何通过 node.js 调用语音 api。

'use strict';

const request = require('request');

const subscriptionKey = 'MYSUBSCRIPTIONKEY';

const uriBase = 'https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US';

const options = {
    uri: uriBase,
    body: 'speech.wav',
    headers: {
        'Content-Type': 'application/json',
        'Ocp-Apim-Subscription-Key' : subscriptionKey,
        'Transfer-Encoding': 'chunked',
        'Expect': '100-continue',
        'Content-type':'audio/wav; codec=audio/pcm; samplerate=16000'
    }
};

request.post(options, (error, response, body) => {
  if (error) {
    console.log('Error: ', error);
    return;
  }
  let jsonResponse = JSON.stringify(JSON.parse(body), null, '  ');
  console.log('JSON Response\n');
  console.log(jsonResponse);
});

标签: javascriptrestazurepostmanspeech-to-text

解决方案


你可以试试这个

fs.readFile('/path/to/my/audiofile.wav', function (err, data) {
  if (err) throw err;
  var options = {
    host: 'https://westus.stt.speech.microsoft.com/speech/recognition/conversation/cognitiveservices/v1?language=en-US',
    method: 'POST',
    headers: { 'Content-Type': 'audio/wav' }
  };
  var req = http.request(options, function(res) {
    // Handle a successful response here...
  });
  req.on('error', function(e) {
    // Handle an error response here...
  });
  // Write the audio data in the request body.
  req.write(data);
  req.end();
});


推荐阅读