简体   繁体   English

HTTP调用以使用Axios / Axios-retry将数据发送到服务器

[英]HTTP call to send data to server using Axios/Axios-retry

I have a JSON object that I wish to send to a server using the server's API key. 我有一个希望使用服务器的API密钥发送到服务器的JSON对象。 I wish to have a retry count of 3 so that I can retry sending data if previous calls fail. 我希望重试次数为3,以便在以前的呼叫失败时可以重试发送数据。

I am not sure whether to use 'axios-retry' or 'retry-axios'. 我不确定是否要使用“ axios-retry”或“ retry-axios”。 How do I configure the Content-Type in the header, and where do I add the API key and the data to be sent. 如何在标头中配置Content-Type,以及在何处添加API密钥和要发送的数据。 My present code looks like this: 我现在的代码如下:

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);
});

Use axios instead, it is a Promise based HTTP client for the browser and node.js 改用axios,它是浏览器和node.js的基于Promise的HTTP客户端

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;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM