Skip to main content

Part of my application retrieves base64 encoded audio data from another API, stores that data as a variable in a data buffer, and then uses that data as the "binary" parameter for the create custom user greeting endpoint. This worked perfectly in Nodejs, I was able to convert the base64 encoded audio data into a stream and pass that into the node form-data like so:

const NodeFormData = require('form-data');
formData = new NodeFormData();


let body = {
type: "Introductory",
answeringRule: {id: "38"}
}

formData.append('json', new Buffer.from(JSON.stringify(body)), {
filename: 'request.json',
contentType: 'application/json'
});


formData.append('binary', audioStream, {
filename: 'audioStream.mp3',
contentType: 'audio/mpeg'
});

However, when refactoring the application for native browser JavaScript, I get the error:

"Failed to execute 'append' on 'FormData': parameter 2 is not of type 'Blob'."

It appears that the native JavaScript FormData must have a Blob object if you are attaching a file. However, when I change the format of the audio data from stream to Blob, I get this error message on the RingCentral end:

"Invalid Attachment Media Type"

It is important to note that the audio data in my application is never saved into a file, only stored in a variable, so I cannot simply attach a file. How can I get this process to work in native JavaScript?

Try this to see if it helps.


I still can't get this to work. Native JavaScript FormData.append() requires a Blob or File object to append audio data to the FormData. However, no matter how I construct this object, I always get

ERROR: 400 (Bad Request)

With the error detail

Uncaught (in promise) Error: Unsupported attachment media type

Here is my most recent code

let body = {
type: "Introductory",
answeringRule: {id: "3036"}
};
formData.append('json', bodyBlob, {
filename: 'request.json',
contentType: 'application/json'
});

formData.append('type', body.type);
formData.append('answeringRuleId', body.answeringRule.

formData.append('binary', new File([audioBuffer], "audio.mp3", {type: "audio/mp3"}), {
filename: 'audioStream.mp3',
contentType: 'audio/mp3'
});


Is there any way around this problem? To construct the Blob or File object in a way that the API will accept it? or is there a way to bypass the FormData all together?


Reply