how to send a post request to speech-to-text v2

28 Views Asked by At

I am trying to send a post-request to the stt v2 rest API with node js but I keep on getting Error: HTTP error! status: 404. I am using one of the service endpoints provided on the stt rest documentation. What am I doing wrong? 

service endpoints: speech service endpoint uri list

documentation: speech-to-text-v2-documentation

const fs = require('fs');

const fetch = require('node-fetch');

const apiKey = 'my_api_key';
const projectId = 'my_project_idr';
const location = 'us-central1';
const model = 'chirp';

const audioFilePath = 'audios/my_audio.mp3';
const audioContent = fs.readFileSync(audioFilePath).toString('base64');

const requestBody = {  
  config: { 
    language_codes: ["en-US"],
    model: "chirp",
    features: {
      "enableWordTimeOffsets": true,
      "enableWordConfidence": true    
    },
  },
    content: audioContent
};

const requestOptions = {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    Authorization: `Bearer ${apiKey}`,
  },
  body: JSON.stringify(requestBody),
};

fetch(`https://us-central1-speech.googleapis.com/v2/recognizer=projects/${projectId}/locations/${location}/recognizers/_:recognize`, requestOptions).then(response => {    
  if (!response.ok) {      
    throw new Error(`HTTP error! status: ${response.status}`);    
  }    
  return response.json();  
})  .then(data => {    
  console.log(data);  
})  .catch(error => {    
  console.error('Error:', error.message);  
});

0

There are 0 best solutions below