Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

got npm module : How to retry for POST requests?

Tags:

node.js

npm

gotjs

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);
});
like image 475
Darshan Naik Avatar asked Feb 25 '19 10:02

Darshan Naik


People also ask

How do I reply to a node POST request?

Server (Node.js) var server = http. createServer(function (request, response) { var queryData = url. parse(request.

How do I handle GET and POST request in node JS?

Note: If you are going to make GET, POST request frequently in NodeJS, then use Postman , Simplify each step of building an API. In this syntax, the route is where you have to post your data that is fetched from the HTML. For fetching data you can use bodyparser package. Web Server: Create app.

What is request module in NPM?

The request module is used to make HTTP calls. It is the simplest way of making HTTP calls in node. js using this request module. It follows redirects by default. Note: As of Feb 11th, 2020, request is fully deprecated.


1 Answers

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

like image 137
Ranjithkumar MV Avatar answered Oct 05 '22 23:10

Ranjithkumar MV