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);
});
Server (Node.js) var server = http. createServer(function (request, response) { var queryData = url. parse(request.
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.
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.
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"] } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With