server side - Using http.request in Node.JS while passing an API key -
i fiddling around node.js try create package postageapp able send emails through our api.
to start, using following code test out how node.js can best interface our api, doesn't seem want pass along api key have attached part of headers.
var http = require('http'); function onrequest(request, response) { response.end(); } http.createserver(onrequest).listen(8888); console.log("server has started."); var options = { host: 'api.postageapp.com', path: '/v.1.0/get_account_info.json', method: 'post', headers: { "api_key" : "my api key here" } }; var req = http.request(options, function(res) { console.log('status: ' + res.statuscode); res.setencoding('utf8'); res.on('data', function (chunk) { console.log('body: ' + chunk); }); }); req.end(); console.log("request sent!");
i pulled using various examples , not - it's not pretty, know. however, using https, got hit our api , response:
{"response":{"status":"unauthorized","message":"invalid or inactive api key used","uid":null}}
the conclusion can come api key not getting passed along, , appreciate how make happen.
thanks!
here's example of code have used call web apis key in header:
var api = http.createclient(80, 'api.example.org'); var request = api.request('get', '/api/foo', { 'host': 'api.example.org', 'accept': 'application/json', 'api-key': 'apikeygoeshere' }); request.on('response', function (response) {}); request.end();
Comments
Post a Comment