javascript - 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);
});
解决方案
你可以试试这个
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();
});
推荐阅读
- r - 添加带有空白条目的两列以及在最后一列中全部空白
- json - Swift JSON 到 Flutter JSON
- ios - 将视图添加到自定义集合视图单元格时出现奇怪的功能
- c# - 公会成员不再填充,不能 GetUserAsync 因为它只返回机器人而没有其他人
- python - 授权标头未确认为所需格式 - Python Cosmos-db api
- python - 如何从模型中获取数据并显示在产品页面上?
- java - 无法将图像上传到 Firebase 存储
- java - 使用协程时出现类型不匹配错误
- rxjs - 如何使 observable 重复
- python - Tkinter 中的条目小部件导致的空白空间