Page 1 of 1
HTTPRequest
Posted: Thu Dec 05, 2019 4:46 pm
by PowerSoft
Hello,
I try to read data from the openweathermap.org. Have good results with python.
I use this simple python code:
Code: Select all
# Enter your API key here
api_key = "e8b010e1ab578d6426ad5a9359f636c1"
# base_url variable to store url
base_url = "http://api.openweathermap.org/data/2.5/weather?"
# Give city name
#city_name = input("Enter city name : ")
city_name = "hellevoetsluis"
# complete_url variable to store
# complete url address
complete_url = base_url + "appid=" + api_key + "&q=" + city_name
# get method of requests module
# return response object
response = requests.get(complete_url)
I try the same in Purebasic with no results.
This is the code:
Code: Select all
;
; ------------------------------------------------------------
;
; PureBasic - Call OpenWeathermap.org
;
; http://api.openweathermap.org/Data/2.5/weather?appid=e8b010e1ab578d6426ad5a9359f636c1&q=hellevoetsluis
;
; (c) Jan Kromhout
;
; ------------------------------------------------------------
;
InitNetwork()
HttpRequest = HTTPRequest(#PB_HTTP_Get, "http://api.openweathermap.org/Data/2.5/weather?appid=e8b010e1ab578d6426ad5a9359f636c1&q=hellevoetsluis")
If HttpRequest
Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
Debug "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
What is wrong with the Purebasic code?
Thanks for any help.
Jan Kromhout
Hellevoetsluis-NL
Re: HTTPRequest
Posted: Thu Dec 05, 2019 5:19 pm
by infratec
Code: Select all
; Enter your API key here
#api_key = "e8b010e1ab578d6426ad5a9359f636c1"
; base_url variable To store url
#base_url = "http://api.openweathermap.org/data/2.5/weather?"
; Give city name
#city_name = "hellevoetsluis"
#complete_url = #base_url + "appid=" + #api_key + "&q=" + #city_name
InitNetwork()
HttpRequest = HTTPRequest(#PB_HTTP_Get, #complete_url)
If HttpRequest
Debug "StatusCode: " + HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode)
Debug "Response: " + HTTPInfo(HTTPRequest, #PB_HTTP_Response)
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
You missed the query

Re: HTTPRequest
Posted: Thu Dec 05, 2019 5:34 pm
by kenmo
Their URL is case-sensitive.
'data' got changed to 'Data' ***
Change it back to '/data/' and it works fine.
*** I'm guessing it got changed by the IDE's aggressive case correction (Data is a keyword) which I would like to change now that the IDE is open source

Re: HTTPRequest
Posted: Thu Dec 05, 2019 5:37 pm
by infratec
Since you will ask later ...
Code: Select all
EnableExplicit
; Enter your API key here
#api_key = "e8b010e1ab578d6426ad5a9359f636c1"
; base_url variable To store url
#base_url = "http://api.openweathermap.org/data/2.5/weather?"
; Give city name
#city_name = "hellevoetsluis"
#complete_url = #base_url + "appid=" + #api_key + "&q=" + #city_name
Structure coordStructure
lon.f
lat.f
EndStructure
Structure weatherList
id.i
main.s
description.s
icon.s
EndStructure
Structure mainStructure
temp.f
pressure.i
humidity.i
temp_min.f
temp_max.f
EndStructure
Structure windStructure
speed.f
deg.i
EndStructure
Structure cloudStructure
all.i
EndStructure
Structure sysStructure
type.i
id.i
country.s
sunrise.i
sunset.i
EndStructure
Structure WeatherStructure
coord.coordStructure
List weather.weatherList()
base.s
main.mainStructure
visibility.i
wind.windStructure
clouds.cloudStructure
dt.i
sys.sysStructure
timezone.i
id.i
name.s
cod.i
EndStructure
Define HTTPRequest.i, JSON$, JSON.i
Define Weather.WeatherStructure
InitNetwork()
HttpRequest = HTTPRequest(#PB_HTTP_Get, #complete_url)
If HttpRequest
If HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode) = "200"
JSON$ = HTTPInfo(HTTPRequest, #PB_HTTP_Response)
Debug "Response: " + JSON$
Debug ""
JSON = ParseJSON(#PB_Any, JSON$)
If JSON
ExtractJSONStructure(JSONValue(JSON), @Weather, WeatherStructure)
Debug "Lon: " + StrF(Weather\coord\lon, 2)
Debug "Lat: " + StrF(Weather\coord\lat, 2)
ForEach Weather\weather()
Debug "Description: " + Weather\weather()\description
Next
Debug Weather\sys\country + " " + Weather\name
Debug "Temp: " + StrF(Weather\main\temp, 2)
Debug "Windspeed: " + Str(Weather\wind\speed)
Debug "Cloud: " + Str(Weather\clouds\all)
FreeJSON(JSON)
EndIf
Else
MessageRequester("Error", "GET failed")
EndIf
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
Re: HTTPRequest
Posted: Thu Dec 05, 2019 6:13 pm
by PowerSoft
Thanks all for the help.
The code was verry helpfull.
CHEERS
Re: HTTPRequest
Posted: Tue Dec 29, 2020 4:11 pm
by l1marik
I updated previous code from infratec to use onecall functionality.
Code: Select all
EnableExplicit
; Enter your API key here
#api_key = "e8b010e1ab578d6426ad5a9359f636c1"
; base_url variable To store url
#base_url = "http://api.openweathermap.org/data/2.5/onecall?lat=49.5780410767&lon=17.4841003418&"
#complete_url = #base_url + "appid=" + #api_key + "&units=metric&lang=cz"
Debug #complete_url
Structure OWMWeatherList
id.i
main.s
description.s
icon.s
EndStructure
Structure OWMWeatherMinutelyList
dt.i
precipitation.f
EndStructure
Structure OWMWeatherStructure_current
dt.i
sunrise.i
sunset.i
temp.f
feels_like.f
pressure.i
humidity.i
dew_point.f
uvi.f
clouds.i
visibility.i
wind_speed.f
wind_deg.i
List weather.OWMWeatherList()
EndStructure
Structure OWMWeatherStructure_hourly
dt.i
sunrise.i
sunset.i
temp.f
feels_like.f
pressure.i
humidity.i
dew_point.f
uvi.f
clouds.i
visibility.i
wind_speed.f
wind_deg.i
weather.OWMWeatherList
pop.f
EndStructure
Structure OWMDailyTemp
day.f
min.f
max.f
night.f
eve.f
morn.f
EndStructure
Structure OWMDailyFeels_Like
day.f
night.f
eve.f
morn.f
EndStructure
Structure OWMWeatherStructure_daily
dt.i
sunrise.i
sunset.i
temp.OWMDailyTemp
feels_like.OWMDailyFeels_Like
pressure.i
humidity.i
dew_point.f
wind_speed.f
wind_deg.i
List weather.OWMWeatherList()
clouds.i
pop.f
rain.f
snow.f
uvi.f
EndStructure
Structure OWMAlerts
sender_name.s
event.s
start.i
End.i
description.s
EndStructure
Structure OWMWeatherCompleteStructure
lat.f
lon.f
timezone.s
timezone_offset.i
current.OWMWeatherStructure_current
List minutely.OWMWeatherMinutelyList()
List hourly.OWMWeatherStructure_hourly()
List daily.OWMWeatherStructure_daily()
List alerts.OWMAlerts()
EndStructure
Define HTTPRequest.i, JSON$, JSON.i
Define Weather.OWMWeatherCompleteStructure
InitNetwork()
HttpRequest = HTTPRequest(#PB_HTTP_Get, #complete_url)
If HttpRequest
If HTTPInfo(HTTPRequest, #PB_HTTP_StatusCode) = "200"
JSON$ = HTTPInfo(HTTPRequest, #PB_HTTP_Response)
Debug "Response: " + JSON$
Debug ""
JSON = ParseJSON(#PB_Any, JSON$)
If JSON
ExtractJSONStructure(JSONValue(JSON), @Weather, OWMWeatherCompleteStructure)
Debug weather\timezone
ForEach Weather\minutely()
Debug "Date: " + FormatDate("%dd.%mm. %hh:%ii", Weather\minutely()\dt + Weather\timezone_offset) + " - " + Weather\minutely()\precipitation
Next
FreeJSON(JSON)
EndIf
Else
MessageRequester("Error", "GET failed")
EndIf
FinishHTTP(HTTPRequest)
Else
Debug "Request creation failed"
EndIf
Re: HTTPRequest
Posted: Tue Dec 29, 2020 5:19 pm
by infratec
found a small bug:
Code: Select all
Structure OWMWeatherStructure_hourly
dt.i
sunrise.i
sunset.i
temp.f
feels_like.f
pressure.i
humidity.i
dew_point.f
uvi.f
clouds.i
visibility.i
wind_speed.f
wind_deg.i
weather.OWMWeatherList
pop.f
EndStructure
should be
Code: Select all
Structure OWMWeatherStructure_hourly
dt.i
sunrise.i
sunset.i
temp.f
feels_like.f
pressure.i
humidity.i
dew_point.f
uvi.f
clouds.i
visibility.i
wind_speed.f
wind_deg.i
List weather.OWMWeatherList()
pop.f
EndStructure
Re: HTTPRequest
Posted: Tue Jan 05, 2021 7:58 pm
by l1marik
I found not bug, but issue with name of one value in OWM JSON. In alert is one value named as reserved word end. Is way to solve it?
Lukas
Re: HTTPRequest
Posted: Tue Jan 05, 2021 8:21 pm
by infratec
The only trick I know:
Code: Select all
JSON$ = ReplaceString(JSON$, #DQUOTE + "end" + #DQUOTE, #DQUOTE + "_end" + #DQUOTE)