Search code examples
javascriptcurlaxiosgoogle-speech-api

How do I transform this curl request with javascript axios


I want to perform a request to the Google speech API, to return the translation of audio I sent to the API

If I use the curl command below, I successfully retrieve the data, but I don't know how to pass it to an Axios request.

curl command:

curl -H "Authorization: Bearer "$(gcloud auth application-default print-access-token) -H "Content-Type: application/json; charset=utf-8" "https://speech.googleapis.com/v1/operations/{speech_name}"

Solution

  •  // creating axios 
     const api = axios.create({
     baseURL: https://speech.googleapis.com/v1/operations,
     crossDomain: true,
     responseType: 'json',
     headers: {
     'Accept': 'application/json',
     'Content-Type': 'application/json',
     'Authorization':'your_key
     },
    });
    
    // calling api
    
    api.get('/speech_name')
    .then(res=>{
    console.log(res);
    }).
    catch('error');
    

    Note : you can use other request method also api.post(url)