I am utilizing API calls from https://openweathermap.org/current
As of now, I have a program that displays the weather after a zip code is inserted. I want to change the program to display weather after a city name is inserted instead of a zip code.
Here is the code I have written for the zip code input which works:
// takes in the zip from the html form, display in // console. Takes in as string,
ex. for zip 02139
var zip = String(req.body.zipInput);
console.log(req.body.zipInput);
//build up the URL for the JSON query, API Key is // secret and needs to be obtained
by signup
const units = "imperial";
const apiKey = "ed83ec6f91552fa762538146eef4e349";
const url = "https://api.openweathermap.org/data/2.5/weather?zip=" + zip + "&units="
+ units + "&APPID=" + apiKey;
Here is the code I have written for the city name which does NOT work:
// takes in the city name from the html form, display in // console. Takes in as
string.
var cityname = String(req.body.citynameInput);
console.log(req.body.citynameInput);
//build up the URL for the JSON query, API Key is // secret and needs to be obtained
by signup
const units = "imperial";
const apiKey = "ed83ec6f91552fa762538146eef4e349";
const url = "http://api.openweathermap.org/geo/1.0/direct?q={city name}&limit=.
{limit}&appid={API key}" + cityname + "&units=" + units + "&APPID=" + apiKey;
How would I properly write the code to get weather information after a city name is inserted instead of a zip code? The code that I have written for the city name is not working like the code I have written for the zip code.
Please give the answer in code! Feel free to copy and paste my code and make corrections where needed. Thank you.
Everything seems to be in the doc https://openweathermap.org/api/geocoding-api#direct_name just replace the orange parts by actual value either using template literal or concatenation (just as you did for zip).
Here you are missing the city name in your url.
PS: Don't forget that city name seems to contains mutliple value
EDIT: thanks @Bravo for the corrections!