1

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', 
           }
  })

sebagl
  • 15
  • 3

1 Answers1

0

What would be the JavaScript equivalent for this Python code? (I am using node-fetch)

You can try the following code, based on the documentation, use JSON.stringify() in POST request to convert payload to JSON formatted string.

   let payload = {
        'displayname': 'long audio synthesis sample',
        'description': 'sample description',
        'locale': 'en-US,
        'voices': [{ 'voicename': 'some-voice' }],
        'outputformat': 'riff-16khz-16bit-mono-pcm',
        'concatenateresult': True,
    };

fetch(''https://{}.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis', {
    method: 'POST',
    body: JSON.stringify(payload),
    headers: { 'Content-Type': 'application/json' }
}).then(res => res.json())
  .then(json => console.log(json));

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

Ecstasy
  • 1,866
  • 1
  • 9
  • 17
  • 1
    Thanks! I would also need to attach a txt file in the request, how could I achieve that? – sebagl Jun 21 '22 at 14:15
  • [How to send a file in request node-fetch or Node?](https://stackoverflow.com/questions/44021538/how-to-send-a-file-in-request-node-fetch-or-node) – Ecstasy Jun 22 '22 at 03:37
  • [415 (Unsupported Media Type) with REST Post request](https://stackoverflow.com/questions/51755025/415-unsupported-media-type-with-rest-post-request) and [Fetch API headers](https://github.com/node-fetch/node-fetch#fetch-options) – Ecstasy Jun 22 '22 at 03:46