api 呼叫和邮递员之间的区别

difference between api call and on postman

我正在使用 Azure 语音休息 api。我用 .wav 文件对 post 人进行了尝试,结果成功 return。但是,当我从 node.js 代码中调用 api 时。它总是 return Unsupported Audio Format 即使我提供相同的音频文件。谁能告诉我他们有什么区别?或者 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);
});

你可以试试这个

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();
});