0

I am trying to use the open weather map API where I simply have an input field where I want to send to the API a city name in order to retrieve some data.

Following the documentation, I cannot find any specific topic where you can just insert the city name as a query parameter, but rather you'd have to send the lattitude and longitude of the desired city.

Does anyone know if I can just simply somehow construct a url with the provided city name , rather than using the coordinates ?

More details can be found for the api here: https://openweathermap.org/.

I am trying something like this: https://api.openweathermap.org/data/2.5/direct?q={city.name}&appid={secret_key} But if I want to retrieve data for 2 hour forecast or 2 days it returns an 40x error.

mplungjan
  • 169,008
  • 28
  • 173
  • 236
Pete
  • 3
  • 2
  • Please add code you have tried and more details on the actual API being called here - if you wrote the API include details regarding that perhaps. – Mark Schultheiss Jun 21 '23 at 11:58
  • More details can be found for the api here: https://openweathermap.org/ Whereas, regarding the request that is being sent to the API, I am trying something like this: https://api.openweathermap.org/data/2.5/direct?q={city.name}&appid={secret_key} But if I want lets say to retrieve data for 2 hour forecast or 2 days it returns an 40x error. – Pete Jun 21 '23 at 12:04
  • [How to catch misspelled cities](https://stackoverflow.com/questions/56288482/how-to-use-try-and-catch-if-the-user-enters-wrong-city-name-with-an-weather-api) – mplungjan Jun 21 '23 at 12:08
  • Please edit your question to add more details and not just add comments that someone may not read – Mark Schultheiss Jun 21 '23 at 12:11

1 Answers1

1

You can use parameter "q" to request and can send the city name as below

https://api.openweathermap.org/data/2.5/forecast?q=CITY_NAME
  • How can I get lets say: 1 hour forecast and 2 day forecast for a city ? How will the request be in this scenario? – Pete Jun 21 '23 at 12:12
  • https://pro.openweathermap.org/data/2.5/forecast/hourly?q={City Name}&appid={API key} for this you may need to subscribe to the plan with Openweather you can check the documentation at https://openweathermap.org/api/hourly-forecast – Balkishan Dhaker Jun 21 '23 at 13:46