得到了npm模块:如何重试POST请求?

时间:2019-02-25 10:05:30

标签: node.js npm

下面的示例用于获取请求,但是如何使其在发布时起作用? 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);
});

1 个答案:

答案 0 :(得分:4)

示例POST请求,重试计数为3。如果要禁用重试,请将重试计数设置为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添加方法,默认情况下got不支持POST重试

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