【问题标题】:How do I transform this curl request with javascript axios如何使用 javascript axios 转换此 curl 请求
【发布时间】:2019-09-30 01:27:29
【问题描述】:
我想向 Google 语音 API 执行请求,以返回我发送到 API 的音频翻译
如果我使用下面的 curl 命令,我成功地检索了数据,但我不知道如何将它传递给 Axios 请求。
卷曲命令:
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}"
【问题讨论】:
标签:
javascript
curl
axios
google-speech-api
【解决方案1】:
// 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');
注意:您也可以使用其他请求方法api.post(url)