Getting the weather via lon and lat for OpenWeather API

I am trying to get the correct weather for a location so I can display it on a dashboard, for a users next trip. I have got the output working if I push into the server code the city, but this has the potential to cause issues if there happened to be more than 1 city with the same name.

As such I really want to use the longditude and latitude, but its coming up with a 400 error.

Here is the working code:-

api_key = "e6a2d01bf5c0234d0335082a7ecb1550"
  root_url = "http://api.openweathermap.org/data/2.5/weather?"
  city_name = city
  lat = latitude
  lon = longditude
  url = f"{root_url}appid={api_key}&q={city_name}"
  data = anvil.http.request(url,json=True)
  print(data)

But if I change it to this:

  api_key = "e6a2d01bf5c0234d0335082a7ecb1550"
  root_url = "https://api.openweathermap.org/data/2.5/onecall?"
  city_name = city
  lat = latitude
  lon = longditude
  url = f"{root_url}lat={lat}lon={lon}&appid={api_key}"
  data = anvil.http.request(url,json=True)
  print(data)

Which is Openweathers example, here
https://api.openweathermap.org/data/2.5/onecall?lat={lat}&lon={lon}&exclude={part}&appid={API key}

I am getting a 400 error?
I tried the original query with the onecall URL and it gave the same 400 error.
I tried the lon/lat query with the original URL and it didnt like that either.

The ? is missing in front of lat and the & in front of lon.

Hi,
I added those and this is what the print output gives me, this part is what my client side is serving to the server side query.

Valladolid
-4.72359991
41.65280151

And this is what the print(url) statement output was.
https://api.openweathermap.org/data/2.5/onecall?lat=-4.72359991&lon=[41.65280151]&appid=e6a2d01bf5c0234d0335082a7ecb1550

I don’t understand what you are saying.

I tried that URL and it seems to be working after removing the square brackets around the longitude.

1 Like

I fixed it! I’d put a comma on the client side code, when I removed it, it worked. Sorry about the confusion :grimacing:

latitude =self.manage_trips_form.repeating_panel_manage.get_components()[0].latitude.text,
longditude =self.manage_trips_form.repeating_panel_manage.get_components()[0].longditude.text
1 Like