expressfullcontact

How to GET json data from API with a header


I'm trying to get JSON data from an API using NodeJS and Express, but it requires a header I'm not sure how to input the header into the get request.

The documentation says:

curl -H"X-FullContact-APIKey:$your_key" 
'https://api.fullcontact.com/v2/person.json?email=bart@fullcontact.com'

How do I do a add a get request with a header? I looked everywhere on Stackoverflow for days, and havent found anything. Everything is for PHP, nothing for NodeJS w/ Express. How can I do this with the Request Node NPM Manager Package


Solution

  • Not sure on how your request code looks like, but this should do it; right?

    var request = require('request');
    
    var options = {
      url: 'https://api.fullcontact.com/v2/person.json?email=bart@fullcontact.com',
      headers: {
        'X-FullContact-APIKey': '$your_key'
      }
    };
    
    function callback(error, response, body) {
      if (!error && response.statusCode == 200) {
        var result = JSON.parse(body);
        console.log(result);
      }
    }
    
    request(options, callback);