HTTPRequest

Just starting out? Need help? Post your questions and find answers here.
PowerSoft
User
User
Posts: 65
Joined: Sun Aug 16, 2015 2:54 pm

HTTPRequest

Post 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
OS X 10.10.5 PB 5.31(x64)
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HTTPRequest

Post 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 :wink:
User avatar
kenmo
Addict
Addict
Posts: 1967
Joined: Tue Dec 23, 2003 3:54 am

Re: HTTPRequest

Post 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 :)
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HTTPRequest

Post by infratec »

Since you will ask later ... :mrgreen:

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
PowerSoft
User
User
Posts: 65
Joined: Sun Aug 16, 2015 2:54 pm

Re: HTTPRequest

Post by PowerSoft »

Thanks all for the help.
The code was verry helpfull.
CHEERS
OS X 10.10.5 PB 5.31(x64)
l1marik
User
User
Posts: 49
Joined: Tue Jun 30, 2020 6:22 am

Re: HTTPRequest

Post 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
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HTTPRequest

Post 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
l1marik
User
User
Posts: 49
Joined: Tue Jun 30, 2020 6:22 am

Re: HTTPRequest

Post 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
infratec
Always Here
Always Here
Posts: 6883
Joined: Sun Sep 07, 2008 12:45 pm
Location: Germany

Re: HTTPRequest

Post by infratec »

The only trick I know:

Code: Select all

JSON$ = ReplaceString(JSON$, #DQUOTE + "end" + #DQUOTE, #DQUOTE + "_end" + #DQUOTE)
Post Reply