I'm trying to build a little Google Pagespeed client in Node, but I'm struggling with the https client. The request always returns with a 302 response, but the exact same url works perfectly in curl and browsers
options = {
host: 'https://www.googleapis.com'
, path: '/pagespeedonline/v1/runPagespeed?url=' + program.uri + '/&prettyprint=false&strategy=' + program.strategy + '&key=' + program.key
}
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);
});
Am I missing something? Tried sending a few different header, but it didn't make much difference
Drop the https://
prefix in host, and you should be good to go. See the docs here.
Here's a working example, just substitute your own URL and API key:
var https = require('https'), key = 'KEY', url = 'URL', strategy = 'desktop'; https.get({ host: 'www.googleapis.com', path: '/pagespeedonline/v1/runPagespeed?url=' + encodeURIComponent(url) + '&key='+key+'&strategy='+strategy }, 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); });