Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

201
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda