Skip to content

Instantly share code, notes, and snippets.

@ykyuen
Created April 8, 2019 18:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ykyuen/44163c20416021f6a98d0afdc536d9bc to your computer and use it in GitHub Desktop.
Save ykyuen/44163c20416021f6a98d0afdc536d9bc to your computer and use it in GitHub Desktop.
handling-http-request-in-go-echo-framework-1-05
package handler
import (
"encoding/json"
"io/ioutil"
"net/http"
"github.com/labstack/echo"
"gitlab.com/ykyuen/golang-echo-template-example/model"
)
func AboutHandler(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,
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment