In the following sections, we will move on and see how we could make HTTP request in Echo server and render the result back to the client. Let’s start with the example we did in the last article.
Setup an API endpoint
Firstly, create a new API endpoint and return a simple JSON which is later called by the Echo server itself.
Create new endpoint in the api folder
Unlike a hardcoded JSON request, our new API endpoint takes the request input from JSON data, form data or query parameters as defined in the model/example_request.go and then return it the the client.
model/example_request.go
1
2
3
4
5
6
package model
type ExampleRequest struct {
FirstName string`json:"first_name" form:"first_name" query:"first_name"`
LastName string`json:"last_name" form:"last_name" query:"last_name"`
}
package handler
import (
"encoding/json""io/ioutil""net/http""github.com/labstack/echo""gitlab.com/ykyuen/golang-echo-template-example/model"
)
funcAboutHandler(c echo.Context) error {
tr := &http.Transport{}
client := &http.Client{Transport: tr}
// Call the api
resp, err := client.Get(
"http://localhost:1323/api/get-full-name?first_name=Kit&last_name=Yuen",
)
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
// Unmarshal the response into a ExampleResponse struct
var exampleResponse model.ExampleResponse
if err = json.Unmarshal(body, &exampleResponse); err != nil {
return err
}
// Please note the the second parameter "about.html" is the template name and should
// be equal to one of the keys in the TemplateRegistry array defined in main.go
return c.Render(http.StatusOK, "about.html", map[string]interface{}{
"name": "About",
"msg": exampleResponse.Msg,
})
}
Test the about page
Start the Echo server and browse the about page. We could see the new greeting message.
The project structure
After making the above changes, the project structure should look like this. (The project is renamed to handling-http-request-in-go-echo-example-1, make sure you set the import statement in the Golang source files correctly if you want to change the project name.)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
handling-http-request-in-go-echo-example-1/
├── api/ # folder of api endpoints
│ ├── get_full_name.go # api for get full name
├── handler/ # folder of request handlers
│ ├── home_handler.go # handler for home page
│ └── about_handler.go # handler for about page
├── model/ # folder of custom struct types
│ ├── example_request.go # struct type of get_full_name request
│ └── example_response.go # hstruct type of get_full_name response
├── vendor/ # dependencies managed by dep
│ ├── github.com/*
│ └── golang.org/*
├── view/ # folder of html templates
│ ├── base.html # base layout template
│ ├── home.html # home page template
│ └── about.html # about page template
├── Gopkg.lock # dep config file
├── Gopkg.toml # dep config file
└── main.go # programme entrypoint
Summary
This article shows you how to setup a simple API endpoint in Echo framework. Since Go is a strongly typed language, we have to convert the JSON into well defined struct type before for we could manipulate its content. For example:
Binding the GET request input to exampleRequest in api/get_full_name.go.
Unmarshal the GET response into exampleResponse in handler/about_handler.go.
To invoke the API call, we could simply make use of the Go standard net/http library.
The complete example could be found on gitlab.com.
About
Boatswain is a SAAS platform where you could VIEW your Docker application Status, ANALYSE the Logs from all the Containers and GET real time Notification on Events.