Saving a POST REST response (file) into local filesystem

I am using text-to-speech API to get text synthesized. The response in a file format. I would like to save this reply locally in an audio file. I have tried various methods but have not been able to retrieve the data, let alone save.
My target deployment is on an iOS device

Background
The TTS service I am using is Azure. The request is in the following format

POST /cognitiveservices/v1 HTTP/1.1

X-Microsoft-OutputFormat: audio-16khz-32kbitrate-mono-mp3
Content-Type: application/ssml+xml
Host: westus.tts.speech.microsoft.com
Content-Length: 225
Authorization: Bearer [Base64 access_token]

<speak version='1.0' xml:lang='en-US'><voice xml:lang='en-US' xml:gender='Male'
    name='en-US-ChristopherNeural'>
        Microsoft Speech Service Text-to-Speech API
</voice></speak>

The response has mime type ‘audio/mpeg’ as per the audio format I have requested. The data is the audio file.

What I have tried

  1. Data Resource and using Create Record (this is the only one with POST method)
    The setup of the Data Resource only allows schema or no data body. If either GET schema or custom schema is selected, the data is sent as JSON. Otherwise, no data will be sent.
  2. Using HTTP flow function
    Request data format can only be ‘json’ or ‘x-www-form-urlencode’. Leaving it blank will result in the error ‘Unknown body type’
  3. Using JS flow function
    Using the code below to do a fetch in a web preview environment , I am able to retrieve the binary data.
    const response = await fetch(url, {
      method: 'POST', 
      headers,  // headers like X-Microsoft-OutputFormat and Content-Type
      body: data
    });
    const buffer = await response.arrayBuffer();
    const retData = new Int8Array(buffer);

However, when run on a mobile device (iOS in my case), I get the error

FileReader.readAsArrayBuffer is not implemented
  1. Download files flow function
    It looks like this only supports GETs without request body
  2. Implement a backend and proxy the REST call
    I have yet to try this. This is my final option, and I would like to avoid this if possible.
    Here, I would transform the POST API which expects a xml body to a GET API using query params so as to be able to interface with Appgyver. I can then utilize Download files.

Thanks in advance.

Hmm. I don’t know if anyone has tried this before, so it’s not clear what would be required for something like this to work.

From what you’ve said and requiring audio/mpeg format for the HTTP request, it does seem like #5 is your only option at the moment. In general third party plugins could probably be used to solve this for you, but that feature is still going to take a while before it’s ready.

Sorry I couldn’t be of more help :confused:

Thanks so much for replying

I have implemented a simple backend using back4app.

Cheers.

1 Like