requestretry如何使用json正文发送请求?

时间:2019-06-14 14:25:13

标签: node.js request

当请求发送一个json正文时,就像这样

const request = require('request')

request.post('https://flaviocopes.com/todos', {
  json: {
    todo: 'Buy the milk'
  }
}, (error, res, body) => {
  console.log(body)
})

当我想使用requestretry时,它具有相同的json字段,但是设置为true或false,那么我如何使用json主体重试发送请求?

var request = require('requestretry');

request({
  url: 'https://api.domain.com/v1/a/b',
  json: true,

  // The below parameters are specific to request-retry
  maxAttempts: 5,   // (default) try 5 times
  retryDelay: 5000,  // (default) wait for 5s before trying again
  retryStrategy: request.RetryStrategies.HTTPOrNetworkError // (default) retry on 5xx or network errors
}, function(err, response, body){
  // this callback will only be called when the request succeeded or after maxAttempts or on error
  if (response) {
    console.log('The number of request attempts: ' + response.attempts);
  }
});

1 个答案:

答案 0 :(得分:1)

在文档中, Request-retry是请求的直接替代,但添加了两个新选项maxAttempts和retryDelay。。您应该能够像使用json: { ... }一样在request中传递JSON。