0

I'm trying to get a response back from an API by sending the token and the header 'content-type': 'application/json', but I don't know where should I put them.

This is my code so far:

var request = require('request');

request.get('google example url', {
//
  'auth': {
    'bearer': '15252727282'
  },
  "headers": {
    "Content-Type": "application/json"
  }
}, function (error, response, body) {
  console.log('error:', error); // Print the error if one occurred 
  console.log('statusCode:', response && response.statusCode); // Print the response status code if a response was received 
  console.log('body:', body);
});

This is what I'm getting back in my console:

error: null
statusCode: 401
body: HTTP Token: Access denied.
goto
  • 7,908
  • 10
  • 48
  • 58
Defoe
  • 371
  • 1
  • 5
  • 17

2 Answers2

0

OK I did it using options as the first parameter and with the following lines:

const options = {  
    url: 'target url',
    method: 'GET',
    headers: {
        "Authorization": "Token token=12425262",
        "Content-type": "application/json"
        
    }
};

request(options, function(err, res, body) {  
    let json = JSON.parse(body);
    console.log(json);
});
Defoe
  • 371
  • 1
  • 5
  • 17
-1

You're getting that error because you have never defined require anywhere on your client-side.

Add this to your project: http://requirejs.org/docs/release/2.2.0/minified/require.js

If you want to use require on the client take a look at this http://requirejs.org/.