Rafa
Rafa

Reputation: 3339

Trying to send header in node

I'm trying to access an api that requires me to send some information in the header in the GET request as described in the image below.

enter image description here

I have my code set up like this, but I'm getting a resp has no method setter error. I've read in various posts and seen examples in other languages where this is done, but I can't quite figure it out in node.

https.get(url, function(resp){
                    resp.setHeader("Content-Type", "json/application");
                    resp.setHeader("Authorization", Key);

                    resp.on('data', function(chunk){
                        sentStr += chunk;
                    });

                    resp.on('end', function(){
                        console.log(sentStr);
                    });
});

Upvotes: 0

Views: 56

Answers (1)

Rabea
Rabea

Reputation: 2024

You are trying to set the headers for the response, while the request is the one that needs to be set. http and https takes either the URL or a set of options to start the call. here is an example

 var https = require('https');

 var options = {
     hostname: "www.google.com",
     port: 443,
     path: '/',
     method: 'GET',
     headers: {
         "Content-Type": "json/application"
         "Authorization" : "KEY YOU NEED TO SUPPORT"
     }
 }

 https.get(options, function(res) {
       console.log("statusCode: ", res.statusCode);
         console.log("headers: ", res.headers);

           res.on('data', function(d) {
                   process.stdout.write(d);
                     });

 }).on('error', function(e) {
       console.error(e);
 });

Upvotes: 1

Related Questions