HTTP调用使用Axios / Axios重试向服务器发送数据
我有一个JSON对象,我希望使用服务器的API密钥发送到服务器。我希望重试次数为3,这样如果以前的调用失败,我可以重试发送数据。
我不确定是否使用'axios-retry'或'retry-axios'。如何在标头中配置Content-Type,以及在何处添加API密钥和要发送的数据。我现在的代码如下:
const axiosRetry = require('axios-retry');
axiosRetry(axios, { retries: 3 });
var data = { /*----My JSON Object----*/ };
axios.post('my url', data, {
headers: {
'Authorization': 'API_Key',
'Content-Type': 'application/json'
}
})
.then(function(response){
console.log(response);
})
.catch(function(error){
console.log(error);
});
回答如下:
使用axios,它是一个基于Promise的HTTP客户端,用于浏览器和node.js
var axios = require('axios')
axios.post(url,data, {
headers: {
'authorization': your_token,
'Accept' : 'application/json',
'Content-Type': 'application/json'
}
}).then(response => {
// return response;
}).catch((error) => {
//return error;
});