Consuming your Golang APIs
We’re going to build on our previous frontend example to add some functions to GET
and POST
from a simple backend service. The source code can be found inside the chapter9/backend
folder; it focuses on two simplified functions that do little more than return a fixed string for GET
and a reversed string based on the POST
request that we sent.
The appGET()
function provides the functionality to perform a GET
operation, while the appPOST()
function provides it for a POST
operation:
func appGET() http.HandlerFunc { type ResponseBody struct { Message string } return func(rw http.ResponseWriter, req *http.Request) { log.Println("GET", req) json.NewEncoder(rw).Encode(ResponseBody{ ...