Created
April 8, 2019 18:00
-
-
Save ykyuen/44163c20416021f6a98d0afdc536d9bc to your computer and use it in GitHub Desktop.
handling-http-request-in-go-echo-framework-1-05
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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