Reputation: 430
This my code for doing a REST call. I have tried with POSTMAN rest call and the url and headers are ok. But with node js I am not able to do this
I have not given the real API key and url here
/**
* HOW TO Make an HTTP Call - GET
*/
// options for GET
var optionsget = {
host : 'abcd.com:80/edwde/wedwed/', // here only the domain name
// (no http/https !)
//port : 443,
//path : '/youscada', // the rest of the url with parameters if needed
headers : getheaders,
method : 'GET' // do GET
};
var getheaders = {
'x-api-key' : 'diudnwod87wedh8778=',
'content-type' : 'application/json;charset=UTF-8'
};
console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');
// do the GET request
var reqGet = https.request(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('GET result:\n');
process.stdout.write(d);
console.info('\n\nCall completed');
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
This is the reference of my code: http://isolasoftware.it/2012/05/28/call-rest-api-with-node-js/
I have edited my code and it is working:
var http = require('http');
/**
* HOW TO Make an HTTP Call - GET
*/
// options for GET
var getheaders = {
'x-api-key' : 'diudnwod87wedh8778=',
'content-type' : 'application/json;charset=UTF-8'
};
var optionsget = {
host : 'abcd.com', // here only the domain name
// (no http/https !)
port : 80,
path : '/edwde/wedwed/', // the rest of the url with parameters if needed
headers : getheaders,
method : 'GET' // do GET
};
console.info('Options prepared:');
console.info(optionsget);
console.info('Do the GET call');
// do the GET request
var reqGet = http.request(optionsget, function(res) {
console.log("statusCode: ", res.statusCode);
// uncomment it for header details
console.log("headers: ", res.headers);
res.on('data', function(d) {
console.info('GET result:\n');
process.stdout.write(d);
console.info('\n\nCall completed');
});
});
reqGet.end();
reqGet.on('error', function(e) {
console.error(e);
});
Upvotes: 1
Views: 3995
Reputation: 1742
What is the output exactly? Also, you reference getheaders in optionsget before having actually declared getheaders.
Upvotes: 3