This tutorial help to access Aviationstack API using Golang Echo framework.The Echo is a fast and popular web framework.We are processing data and sending response using Echo Http methods.
The Aviationstack API was built to provide a simple way of accessing global aviation data for real-time and historical flights as well as allow customers to tap into an extensive data set of airline routes and other up-to-date aviation-related information. Requests to the REST API are made using a straightforward HTTP GET URL structure and responses are provided in lightweight JSON format.
The sample request –https://api.aviationstack.com/v1/flights?access_key=YOUR_ACCESS_KEY
How To Access API Using Golang Echo Framework
We will create /aviationstack-example
folder into the golang workplace.I have created main.go
file into the d:/workplace_go/aviationstack-example
.
There are following dependencies are used into this golang tutorial –
"encoding/json" "fmt" "github.com/labstack/echo" "github.com/labstack/echo/middleware" "net/http"
No open command line and run go command to download all dependencies –d:/workplace_go/aviationstack-example> go get
Let’s create entry method into the main.go
file, Added below code into this file –
func main() { e := echo.New() //CORS e.Use(middleware.CORSWithConfig(middleware.CORSConfig{ AllowOrigins: []string{"*"}, AllowMethods: []string{echo.GET, echo.HEAD, echo.PUT, echo.PATCH, echo.POST, echo.DELETE}, })) }
echo.New()
method is used to create ECHO
instance and assigned to e
variable. CORSWithConfig
middleware help to enable CORS with in this golang application.
We will create PSdata
Struct as per response JSON data of flight rest endpoints data,
type PSdata struct { Data []struct { Latitude float64 `json:"latitude"` Longitude float64 `json:"longitude"` Type string `json:"type"` Distance int `json:"distance"` Name string `json:"name"` Number string `json:"number"` PostalCode string `json:"postal_code"` Street string `json:"street"` Confidence int `json:"confidence"` Region string `json:"region"` RegionCode string `json:"region_code"` County interface{} `json:"county"` Locality string `json:"locality"` AdministrativeArea interface{} `json:"administrative_area"` Neighbourhood string `json:"neighbourhood"` Country string `json:"country"` CountryCode string `json:"country_code"` Continent string `json:"continent"` Label string `json:"label"` } `json:"data"` }
Now, I will create HTTP Get method to access Aviationstack API. I have created PSdata struct and used to set data of response.
e.GET("/flights", func(c echo.Context) error { // Build the request req, err := http.NewRequest("GET", "https://api.aviationstack.com/v1/flights/", nil) if err != nil { fmt.Println("Error is req: ", err) } // create a Client client := &http.Client{} // Do sends an HTTP request and resp, err := client.Do(req) if err != nil { fmt.Println("error in send req: ", err) } // Defer the closing of the body defer resp.Body.Close() // Fill the data with the data from the JSON var data PSdata // Use json.Decode for reading streams of JSON data if err := json.NewDecoder(resp.Body).Decode(&data); err != nil { fmt.Println(err) } return c.JSON(http.StatusOK, data) })
We have created ‘/flights’ end point that will access from browser like 'https://api.aviationstack.com/v1/flights/'
. Created new GO HTTP request using NewRequest()
method. The NewRequest()
method takes three parameters HTTP request type, HTTP request URL and request body if any.
Created HTTP client using &http.Client{}
, send HTTP request using .Do()
method that takes HTTP Request as param, after successful HTTP request closed the request body.We are using GO json package to decode and read json response.
Finally send json data with http status code to requester.
The post Aviationstack API Example Using Golang Echo appeared first on Rest Api Example.