【问题标题】:HTTP call to send data to server using Axios/Axios-retry使用 Axios/Axios-retry 向服务器发送数据的 HTTP 调用
【发布时间】:2019-02-14 21:17:21
【问题描述】:

我有一个 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);
});

【问题讨论】:

    标签: node.js axios


    【解决方案1】:

    改用 axios,它是一个基于 Promise 的浏览器和 node.js 的 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;
    });
    

    【讨论】:

    • 这可行,但无法重试。如果出现类似 axios-retry 的错误,是否有重试的选项?
    • 您尝试将相同的配置添加到您的 axios 请求中?在 header 中添加 API_KEY 和 Content-Type 并尝试。
    猜你喜欢
    • 2017-06-15
    • 1970-01-01
    • 2021-12-09
    • 2020-03-22
    • 2023-03-03
    • 2021-12-20
    • 1970-01-01
    • 2020-02-22
    • 2021-12-20
    相关资源
    最近更新 更多