I am trying to make a simple POST request to synthesize speech from plain text using AWS Polly REST API in browser javascript. I am not using the AWS JS SDK due to some external reasons. This is my request:
$.ajax({
url: 'https://polly.us-west-2.amazonaws.com/v1/speech',
type: 'POST',
data:'{"OutputFormat":"mp3","Text":"Some text to listen","TextType":"text","VoiceId":"Joanna"}',
dataType: 'text',
beforeSend: function(xhr){
xhr.setRequestHeader('Authorization', '<String>');
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
},
success: function(result){
console.log(result);
}
});
The request succeeds but when printing the result I receive the following:
ID3#TSSELavf57.56.101��`�ù�CNX�DDDGwws����'��wDDDB��D/��?����!+��������....... a bunch of random data.
I tried encoding it and manipulating it in some way but nothing worked. I went through all the AWS Polly documentation, most of the stackoverflow posts but no result. The docs says that I need to receive AudioStream in a specific format my response is just an unreadable string.
Any ideas?
Thank you!
Here is the docs if they can help you understand the problem better: http://docs.aws.amazon.com/polly/latest/dg/API_SynthesizeSpeech.html
Updated
The problem was fixed by changing the response typo to blob without using ajax but instead doing it with a native javascript POST request.
var xhr = new XMLHttpRequest();
xhr.addEventListener("readystatechange", function(){
// something
}
xhr.open('POST')
xhr.setRequestHeader(...)
xhr.responseType = 'blob'