Kong / unirest-nodejs

Unirest in Node.js: Simplified, lightweight HTTP client library.
http://unirest.io/nodejs
MIT License
953 stars 167 forks source link

Are global headers possible? #110

Open 4F2E4A2E opened 7 years ago

4F2E4A2E commented 7 years ago

Hi there, thank you for unirest!

See following example on how we are defining the header information:

const unirest = require('unirest');

class RouterService {

  private auth = 'Basic sdofihsdfjkn3873kajsd‘;
  private dataURL = 'http://backend-xyz‘;

  getData() {
    return unirest.get(`${dataURL}/data`)
    .headers({'Authorization': this.auth});
  }

  postData() {
    return unirest.post(`${dataURL}/data`)
    .headers({'Authorization': this.auth});
  }

}

Is there a way to set the headers information globally? The goal is not to define the header values for each and every request on a express route.

Thank you in advance.

4F2E4A2E commented 7 years ago

A simple yes - with an example ^^ - or a no is enough for me :)

DeadcatDev commented 6 years ago

Want it too :) For now im just using smth like:

const globalHeader = {some:object};

unirest.headers(globalHeader)[...]

in my requests or kind of this._buildHeaders() inside. Sucks but all requests looks the same so no so much DRY at it was.