So I've been messing around with the Spotify API - for the sake of this post, I'm going to use the pokemon API in my code because it's public.When I try using options with https.request, I ALWAYS getgetaddrinfo ENOTFOUND pokeapi.co/api/v2/ pokeapi.co/api/v2/:443This happens for any API I try and use - it's clearly a problem with my code but I can't see the issue. If I use the URL directly as a parameter in https.request (with no options), the request goes through just fine. Hopefully someone here can illuminate what's going on!WORKING CODE:var request = https.request("https://pokeapi.co/api/v2/pokemon/ditto", function (res) {console.log('made contact with API')let dataAggregator = ''res.on('data', function (chunk) {dataAggregator += chunk})res.on('end', function () {try {const parsedData = JSON.parse(dataAggregator)console.log(parsedData)} catch (e) {console.error(e.message)}})}).on('error', function (e) {console.error(e.message)})request.end()BROKEN CODE:const https = require('https')const options = {hostname: 'pokeapi.co/api/v2',path: '/pokemon/ditto',}var request = https.request(options, function (res) {console.log('made contact with API')let dataAggregator = ''res.on('data', function (chunk) {dataAggregator += chunk})res.on('end', function () {try {const parsedData = JSON.parse(dataAggregator)console.log(parsedData)} catch (e) {console.error(e.message)}})}).on('error', function (e) {console.error(e.message)})request.end()
Submitted February 14, 2019 at 10:33PM by ksatia
No comments:
Post a Comment