【问题标题】:got npm module : How to retry for POST requests?得到 npm 模块:如何重试 POST 请求?
【发布时间】:2021-06-01 04:50:39
【问题描述】:

以下示例适用于 GET 请求,但不适用于 POST 请求。如何使它适用于 POST?

https://www.npmjs.com/package/got#retry

const got = require('got')
const retry = {
  retry: {
    retries: 3
  }
}
got('http://localhost:3000/retry', retry).then(({ body }) => {
  console.log(body);
}).catch((err) => {
  console.log(err);
});

【问题讨论】:

标签: node.js npm gotjs


【解决方案1】:

重试次数为 3 的示例 POST 请求。如果要禁用重试,请将重试次数设置为 0。

const got = require('got');
start()
async function start() {
 var response = await request()
 console.log(response);  
}

async function request() {
try {
    const response = await got.post('https://example.com', { retry: { limit: 3, methods: ["GET", "POST"] } });
    return response.body
 } catch (error) {
    console.log(error.response.body);   
    return error
 }
}

如下图的POST添加方法,get默认不支持POST重试

got.post('https://example.com', { retry: { limit: 1, methods: ["GET", "POST"] } });

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-09
    • 1970-01-01
    • 2016-12-24
    • 2016-06-12
    • 2019-06-29
    • 2021-11-05
    • 2017-11-13
    • 1970-01-01
    相关资源
    最近更新 更多