This tutorial help to Access Rest 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.
I am consuming Employee API using HTTP Get call.This API provides array of employee data.
The sample request –
http://dummy.restapiexample.com/api/v1/employees
How To Access Rest API Using Golang
We will create /golang-http-get-example folder into the golang workplace.I have create main.go file into the d:/workplace_go/golang-http-get-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"
Open command line and run go command to download all dependencies –
d:/workplace_go/golang-http-get-example> go get
The go get command is used to download all packages. You can also download a single package using below command –
d:/workplace_go/golang-http-get-example> go get -u github.com/labstack/echo/
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 Emps Struct as per response JSON data of flight rest endpoints data,
type Emps []struct {
ID string `json:"id"`
EmployeeName string `json:"employee_name"`
EmployeeSalary string `json:"employee_salary"`
EmployeeAge string `json:"employee_age"`
ProfileImage string `json:"profile_image"`
}
Now, I will create HTTP Get method to access dummy API.I have created Emps struct and used to set data of response.
e.GET("/employees", func(c echo.Context) error {
// Build the request
req, err := http.NewRequest("GET", "http://dummy.restapiexample.com/api/v1/employees", 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 Emps
// 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)
})
I have created '/employees' end point that will access from browser like 'http://localhost:5000/employees'. 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.
