anvil.http.HttpRequestFailed: host is null

I’m trying to get weather data from the open weather API.

I used my own code to generate the API key, as the standard one kept exceeding the DarkSky quota, but now it doesn’t work for a different reason. It does work however if I input it in my browser manually.

Code Sample:
@anvil.server.callable
def get_weather_data(latitude, longitude):
try:
resp = anvil.http.request(“api.openweathermap.org/data/2.5/forecast?lat=%s&lon=%s&appid=%s&units=metric
% (latitude, longitude, “20b156b0d7aa182850baffb16bfc5402”),
json=True)
# convert timestamp to datetime object
time = datetime.fromtimestamp(resp[‘current’][‘dt’])
# return time and temperature data
return {
‘time’:time,
‘temp’:resp[‘current’][‘temp’]
}
except anvil.http.HttpError:
# If we’ve exceeded our DarkSky quota, we return some random data for this example
return {‘time’:datetime.now(), ‘temp’:65 + random.uniform(-3, +3)}

Clone link:
https://anvil.works/build#clone:RVR2I3RCAHCLFMFL=BKPLXD22K5ULTHRYDVCKT2II

I think you need to have https:// before api.openweathermap.org in line 58.

The next issue is that you will get a KeyError on line 63 but I think this should get you going

Edit: Welcome to the forum @r.f.degen

1 Like

Thank you, the https:// was indeed the issue, but there were a lot more issues as you’ve said. tl;dr: it works now.

1 Like