Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

205
Vistas
How do I make API call for city name instead of zip code?

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.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

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!

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda