Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Question : change defaults() options #337

Open
ghost opened this issue Dec 4, 2019 · 0 comments
Open

Question : change defaults() options #337

ghost opened this issue Dec 4, 2019 · 0 comments

Comments

@ghost
Copy link

ghost commented Dec 4, 2019

Hi,

My app needs an authentication token to request an API.
This token must be included in the header of each request (except the authentication request). Because it can expire my application must be able to request a new one (and must therefore remove the old one from the headers to do it)

So, is it possible to modify the options declared with the default() method ? Or must I store my token in a varriable and include the header in each request ?

Thanks in advance

An example of what I need

const rp = require('request-promise').defaults({
  uri: "myUrl",
  method: 'POST',
  headers: {
    'x-app': 'myApp',
  },
});

// manage (define, modify, ...) the header token
function login() {
  const options = { body: { 'a specific body Object' } };

  // if exists, delete the current token from headers
  if(rp.defaults.headers['my-token'])
    delete rp.defaults.headers['my-token'];
  
  return rp(options).then(result => {
    // add the new token in the defaults headers
    rp.defaults.headers['my-token'] = result.token

    // something else
  });
}

// token is in defaults options
function anotherFunction() {
  const options = { body: { 'an other body Object'} };
  return rp(options).then(result => {
    // do something else
  });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

0 participants