Troubleshooting POSTing to text-to-speech API from command line

I’m looking to troubleshoot using the text-to-speech API. This is my first time using google cloud, so I had to signup, and I enabled billing, and I am able to get the speech synthesize service working through my browser with the cloud console.

My goal is to synthesize batches of text files with a python script. I’m on linux and have installed Google Cloud CLI, and I have been able to authorize using gcloud auth aplication-default login.

I’ve been following these instructions:

https://cloud.google.com/text-to-speech/docs/create-audio-text-command-line

When I execute the REST request, I get the prompt, “Please enter content (application-www-form-urlencoded) to be POSTed:” I have my request body saved as request.json, but when I execute the command to POST, I don’t get any return. Am I missing a step, or can anyone help me troubleshoot? Thank you in advance.

Good day @jpd633 ,

Welcome to Google Cloud Community!

This may have happened due to the options you’ve used when using the curl command, you can try following the options I’ve used:

curl -X POST     
-H "Authorization: Bearer $(gcloud auth print-access-token)"     
-H "x-goog-user-project:<INSERT-YOUR-PROJECT-ID>"     
-H "Content-Type: application/json; charset=utf-8"     
-d @request.json     
"https://texttospeech.googleapis.com/v1/text:synthesize"

The request.json contains the sample request in the documentation you’ve sent: https://cloud.google.com/text-to-speech/docs/create-audio-text-command-line

{
  "input":{
    "text":"Android is a mobile operating system developed by Google, based on the Linux kernel and designed primarily for touchscreen mobile devices such as smartphones and tablets."
  },
  "voice":{
    "languageCode":"en-gb",
    "name":"en-GB-Standard-A",
    "ssmlGender":"FEMALE"
  },
  "audioConfig":{
    "audioEncoding":"MP3"
  }
}

To save the output to a file, you can use > at the end. See sample the below:

curl -X POST     
-H "Authorization: Bearer $(gcloud auth print-access-token)"     
-H "x-goog-user-project:<INSERT-YOUR-PROJECT-ID>"     
-H "Content-Type: application/json; charset=utf-8"     
-d @request.json     
"https://texttospeech.googleapis.com/v1/text:synthesize" > synthesize-out.txt

After that you can decode the contents of synthesize-out.txt file to mp3 file, based from the documentation you have sent.

base64 SOURCE_BASE64_TEXT_FILE -d > DESTINATION_AUDIO_FILE

if you encountered an invalid input error, try including i in your options:

base64 SOURCE_BASE64_TEXT_FILE -di > DESTINATION_AUDIO_FILE​

Since your goal is to synthesize batches of text files thru python scripts, you can use this guide on how to make a request using client libraries (Python) to Text-to-Speech: https://cloud.google.com/text-to-speech/docs/create-audio-text-client-libraries#client-libraries-install-python

Hope this helps!

Thank you for the instructions. Now I am getting an empty txt file named synthesize-out. Here are the commands I’m using. Can you help me troubleshoot further?

gcloud auth application-default login

gcloud auth print-access-token

POST https://texttospeech.googleapis.com/v1/text:synthesize

curl -X POST
-H “Authorization: Bearer {token}”
-H “x-goog-user-project: johnpatdougherty:”
-H “Content-Type: application/json; charset=utf-8”
-d @request.json
https://texttospeech.googleapis.com/v1/text:synthesize” > synthesize-out.txt