Last active
July 18, 2019 00:33
-
-
Save dexterlabora/fef1f7de9ab4d95d68efa89763ed5cfd to your computer and use it in GitHub Desktop.
A Meraki Dashboard API request wrapper to follow 301/302/307/308 redirects properly.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Handles Meraki API requests. Has additional logic to follow the HTTP redirects properly and handle OrgID being converted to INTs | |
var request = require("request"); | |
var JSONbig = require("json-bigint")({ storeAsString: true }); | |
// Recursive function to follow Meraki API redirects | |
var requestMeraki = function(options, callback) { | |
request(options, function(error, res, data) { | |
//console.log('RESPONSE [ ' + res.statusCode + ' ]'); | |
if (error) { | |
return callback(error); | |
} else { | |
if ( | |
[301, 302, 307, 308].indexOf(res.statusCode) !== -1 && | |
res.headers.location | |
) { | |
console.log("REDIRECT: (recursive function)"); | |
options.url = res.headers.location; | |
return requestMeraki(options, function(err, res, data) { | |
return callback(err, res, data); | |
}); | |
} else { | |
// parse the large integers properly if data exists | |
try { | |
var parsedData = JSONbig.parse(data); | |
return callback(error, res, parsedData); | |
} catch (e) { | |
console.log("error: no data returned ", error); | |
} | |
//console.log("FINISHED") | |
return callback(error, res, data); | |
} | |
} | |
}); | |
}; | |
module.exports = requestMeraki; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example of using the request-meraki module: