Azure长音频API -NODEJS
我正在尝试向Azure Speech Services API提出帖子请求,以将文本转换为语音。该请求需要发送JSON有效负载和.TXT文件。问题是我正在建立一个nodejs后端,并且文档仅在Python中。
此Python代码的JavaScript等效是什么? (我正在使用node-fetch)
url = 'some-url'
header = {
'Ocp-Apim-Subscription-Key': 'some-key'
}
payload = {
'displayname': 'long audio synthesis sample',
'description': 'sample description',
'locale': 'en-US,
'voices': [{ 'voicename': 'some-voice' }],
'outputformat': 'riff-16khz-16bit-mono-pcm',
'concatenateresult': True,
}
filename = ntpath.basename('some-path/file.txt')
files = {
'script': (filename, open('some-path/file.txt', 'rb'), 'text/plain')
}
response = requests.post(url, payload, headers=header, files=files)
已经尝试了以下代码以在请求中包含txt文件,但我在响应“ statustext:'不支持的媒体类型'”中得到了响应。
const payload = {
"displayname": 'test',
"description": 'audio',
"locale": 'en-US',
"voices": [{ 'voicename': 'some-voice' }],
"outputformat": 'riff-16khz-16bit-mono-pcm',
"concatenateresult": true,
}
const files = {
'script': (fs.readFileSync(text), 'text/plain')
}
await fetch('some-url', {
method: 'POST',
files,
payload: JSON.stringify(payload),
headers: {
'Ocp-Apim-Subscription-Key': 'some-key',
'Content-Type': 'application/json',
}
})
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以根据文档,在邮政请求中使用
json.stringify()
将有效载荷
转换为json格式化字符串。您可以参考带有邮政请求的节点 - fetch问题“ https://coderrocketfuel.com/article/make-an-http-post-post-request-with-node-node-node-fetch-and-node-node-node-js” rel =“ nofollow noreferrer”>与node-fetch&amp&amp&ampshe ; node.js 和
You can try the following code, based on the documentation, use
JSON.stringify()
in POST request to convertpayload
to JSON formatted string.You can refer to Node-fetch problems with POST requests, Make an HTTP POST Request With Node-Fetch & Node.js and LongRunning - cognitive-services-speech-sdk-js