I am trying to make a POST request to Azure speech services API to convert text to speech. The request needs to send a JSON payload and a .txt file. The thing is that I am building a NodeJS backend and the documentation is only in Python.
What would be the JavaScript equivalent for this Python code? (I am using 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)
Already tried the following code to include the txt file in the request but I got in response "statusText: 'Unsupported Media Type'"
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',
}
})