Weather Data not printing in my command line
I tried to print the weatherData as shown in the image from the URL, but it's not printing out in the console. What could the problem and how do I rectify it?
CodePudding user response:
Two problems with your code (besides that it comes as a screenshot):
- Lines 11 and 14 refer to different
responseobjects. - The
dataevent delivers only one chunk of data; there may be several, followed by anendevent.
https.get(url, function(response) {
console.log(response.statusCode);
var weatherData = "";
response.on("data", function(data) {
weatherData = data.toString();
}).on("end", function() {
console.log(JSON.parse(weatherData));
});
});
CodePudding user response:
By far the easiest way to download is use Windows native Curl command which you can run via a Cmd console window, to save the response file, then query that response.txt
First confirm your url is valid (click link below)
If you are using windows 10 or 11 you should have a native version of curl simply try this at cmd line (NOTE you need for each & within the url to escape with ^ like this ^&)
curl -o response.txt https://api.openweathermap.org/data/2.5/weather?q=Paris^&appid=536bcef96b2f01cd9b9f076db90807fe^&unit=metric
type response.txt
you could include both in one line but for the &type that last & does not need ^escape
curl -o response.txt https://api.openweathermap.org/data/2.5/weather?q=Paris^&appid=536bcef96b2f01cd9b9f076db90807fe^&unit=metric&type response.txt
after the download you should see the response.txt in the console.
so you can call that command any way you wish (hidden>nul or not) and or read the text file on the screen or in any application you choose.
Response.txt
{"coord":{"lon":2.3488,"lat":48.8534},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"base":"stations","main":{"temp":277.1,"feels_like":277.1,"temp_min":275.8,"temp_max":278.23,"pressure":1009,"humidity":98},"visibility":10000,"wind":{"speed":1.03,"deg":0},"clouds":{"all":100},"dt":1641768056,"sys":{"type":2,"id":2012208,"country":"FR","sunrise":1641714129,"sunset":1641744764},"timezone":3600,"id":2988507,"name":"Paris","cod":200}
