You can use the OpenWeatherMap in Go using NewRequest.
From any page on the RapidAPI Marketplace, click “Sign Up” and register for a free account.
Get to the OpenWeatherMap API Page by clicking here or searching for it in the RapidAPI marketplace search bar.
Next, click on the API’s Pricing Tab and select a plan to subscribe to. The OpenWeatherMap API has 2 pricing plans on RapidAPI:
After subscribing to a pricing plan, head back to the endpoints page and choose an endpoint, fill out the required parameters, and click “Test Endpoint”.
If done correctly, on the right side of the API console, you should see a response like this:
Now that you have successfully tested that the API works, click on the Code Snippet dropdown and select one of the following:
You’ll see something similar to this:
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://community-open-weather-map.p.rapidapi.com/weather?q=London%252Cuk" req, _ := http.NewRequest("GET", url, nil) req.Header.Add("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") req.Header.Add("x-rapidapi-key", "[your rapidapi key]") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }
Modify as needed and drop it into your application.
Voila, you’re done!
The OMW API has 4 endpoints available on RapidAPI:
Description: Using this kind of requests you can get weather data in any location on the earth. The current weather data are updated online based on data from more than 40,000 weather stations.
Example request:
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://community-open-weather-map.p.rapidapi.com/weather?q=London%252Cuk" req, _ := http.NewRequest("GET", url, nil) req.Header.Add("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") req.Header.Add("x-rapidapi-key", "[your rapidapi key]") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }
Description: 16-day forecasts are available for any location or city. Forecasts include daily weather and available in JSON or XML format. It is only available for all paid accounts.
Example request:
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://community-open-weather-map.p.rapidapi.com/forecast/daily?q=san%20francisco%252Cus" req, _ := http.NewRequest("GET", url, nil) req.Header.Add("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") req.Header.Add("x-rapidapi-key", "[your rapidapi key]") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }
Description: By city name. Input the city name or its part and get the list of the most proper cities in the world. Example – Lon or Lond or London. The more precise city name you put the more precise list you will get. To make it more precise put the city’s name or its part, comma, the name of the county or 2-letter country code. You will get all the proper cities in the chosen county. The order is important – the first is city name then a comma then the county. Example – Lon, UK or Lon, GB or London, GB or Lon, England. By geographic coordinates.
Example request:
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://community-open-weather-map.p.rapidapi.com/find?units=imperial%252C%20metric&q=london" req, _ := http.NewRequest("GET", url, nil) req.Header.Add("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") req.Header.Add("x-rapidapi-key", "[your rapidapi key]") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }
Description: A 5-day forecast is available for any location or city. It includes weather data every 3 hours. The forecast is available in JSON or XML format.
Example request:
package main import ( "fmt" "net/http" "io/ioutil" ) func main() { url := "https://community-open-weather-map.p.rapidapi.com/forecast?q=san%20francisco%252Cus" req, _ := http.NewRequest("GET", url, nil) req.Header.Add("x-rapidapi-host", "community-open-weather-map.p.rapidapi.com") req.Header.Add("x-rapidapi-key", "[your rapidapi key]") res, _ := http.DefaultClient.Do(req) defer res.Body.Close() body, _ := ioutil.ReadAll(res.Body) fmt.Println(res) fmt.Println(string(body)) }