1

When using the NPM request module's request.defaults API, does anyone know of anyway in which I can pass a function as a request header, so that the function's result becomes the header value everytime I make an outgoing request?

i.e.

request.defaults({
    headers:{datestamp:() => new Date()
})
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80
maxcbc
  • 31
  • 2
  • 7

1 Answers1

6

It works with a getter method. You also need to use the instance returned from .defaults.

const Request = require('request');
const request = Request.defaults({
    headers: {
        get timestamp() { return new Date(); }
    }
})
request.get('/foo', ...);
Ben Fortune
  • 31,623
  • 10
  • 79
  • 80