1

I'm trying to access the text-to-speech service provided by IBM Watson in my NodeJs application but I see access denial error as the service has been blocked by CORS policy. Tried all the ways by setting up related headers in app.js but fail to resolve the issue.

https://cloud.ibm.com/apidocs/text-to-speech?code=node

app.use(function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "http://localhost:3000");
  res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
  next();
});

Whenever I send any text from my NodeJS application a speech should be heard saying the relative response.

1565986223
  • 6,420
  • 2
  • 20
  • 33
Yashwanth
  • 11
  • 2
  • `https://cloud.ibm.com/apidocs/text-to-speech?code=node` If you do not own this resource and owner have set restrictive `CORS` policy, answer to your question is **not possible** – 1565986223 Apr 06 '19 at 10:27

1 Answers1

1

CORS errors should only occur if you are trying to invoke the service from browser side javascript. Having said that I was under the impression that Text to Speech was one of the Watson services that did support CORS

If you invoke the service from the server side of the node.js application then that should be able to invoke the text to speech to service without any CORS restriction.

In short if you are invoking the service correctly then you should not be seeing any CORS errors. Which suggests that you are not invoking the service correctly. Based on the API documentation - https://cloud.ibm.com/apidocs/text-to-speech?code=node

Your code should look something like:

var TextToSpeechV1 = require('watson-developer-cloud/text-to-speech/v1');
var fs = require('fs');

var textToSpeech = new TextToSpeechV1({
  iam_apikey: '{apikey}',
  url: '{url}'
});

var synthesizeParams = {
  text: 'Hello world',
  accept: 'audio/wav',
  voice: 'en-US_AllisonVoice'
};

// Pipe the synthesized text to a file.
textToSpeech.synthesize(synthesizeParams).on('error', function(error) {
  console.log(error);
}).pipe(fs.createWriteStream('hello_world.wav'));

where your url endpoint should be something like https://stream.watsonplatform.net/text-to-speech/api

chughts
  • 4,210
  • 2
  • 14
  • 27