1

I'm trying to switch from request.js to got.js. I expect to see the got.js implementation authenticate similarly to how the request.js library does. But, instead, I get the following error.

auth no longer supported. Replaced by username and password.

There is no mention of bearer tokens on the docs page.

So how do I authenticate my request using bearer tokens using got.js? Or what am I doing wrong?

Current code: request.js, working
const request = require('request');
const module.exports = config => {
  const options = {
    auth: {
      bearer: config.secret,
    },
  };
  const result = await new Promise(( resolve, reject, ) => {
    request.get( url, options, ( error, response, body, ) => {
  ...
New code: got.js, throws error
const got = require('got');

module.exports = async config => {
  const options = {
    auth: {
      bearer: config.secret,
    },
  };
  const result = await got(url, options);
  ...
}
Let Me Tink About It
  • 15,156
  • 21
  • 98
  • 207

2 Answers2

5

This should be worked, if I'm not wrong

let token = 'your token'
const options = {
  headers: {
    'Authorization': `Bearer ${token}`
  }
};
Ahmet Zeybek
  • 1,196
  • 1
  • 13
  • 24
0

worked for me !!

router.get('/product', (req,res)=>{
     const dataStream =  got.stream({
      url: "http://localhost:8000/products",
      method: "GET",
    
        hooks: {
        beforeRequest: [
            options => {
                var token= 'Bearer ' + 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpYXQiOjE2MTkzODA0NjIsImV4cCI6MTYxOTM4NDA2Mn0.JoJbRpPniGuMbwULEtts7I19QxEImvarT6AoqVuNb9w'
                options.headers['Authorization'] = token;

            }
        ]
    }
     });

pipeline(dataStream, res, (err) => {
      if (err) {
          console.log(err);
          res.sendStatus(500);
      }
  });
});