Consuming the RESTful API
The RESTful API is the most common way applications and servers provide access to their services. This recipe will show you how it can be consumed with the help of a HTTP client from the standard library.
How to do it...
- Open the console and create the folder
chapter07/recipe09
. - Navigate to the directory.
- Create the
rest.go
file with the following content:
package main import ( "encoding/json" "fmt" "io" "io/ioutil" "net/http" "strconv" "strings" ) const addr = "localhost:7070" type City struct { ID string Name string `json:"name"` Location string `json:"location"` } func (c City) toJson() string { return fmt.Sprintf(`{"name":"%s","location":"%s"}`, c.Name, c.Location) } func main() { s := createServer(addr) go s.ListenAndServe() ...