简体   繁体   中英

got npm module : How to retry for POST requests?

The below example is for GET requests, but does not work for POST requests. How can I make it work for 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);
});

Sample POST request with retry count as 3. If you want to disable retry set retry count to 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
 }
}

For POST Add Methods as shown below, by default got does not support retries for POST

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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