Following examples to consuming a REST API by using the lib net/http.
NOTE: You will find how to convert the Golang struct to JSON format as the response in JSON format to the Golang struct as well.
Struct Example
The following struct will be used as an example to manipulated the data.
//Struct example type Client struct{ Name string `json:"name"` Address string `json:"address"` Orders []Order `json:"order,omitempty"` }
POST Example
The following example will show how to transform an instance of a Golang struct/object in JSON format to do the request as well.
// Create the object and parse for JSON clientJSON, err := json.Marshal(client) if err != nil { fmt.Errorf("Error to transform the client struct in JSON", "client", client, "Error", err) } //Create the POST request url:= "http://myrestservice/api/clients" req, err := http.NewRequest(http.MethodPost, url , strings.NewReader(string(clientJSON))) req.Header.Set("Content-Type", "application/json") if err != nil { fmt.Errorf("Error when try to create the request", "HTTPMethod", http.MethodPost, "Request", req, "url", url, "error", err) } //Do the request client := &http.Client{} response, err := client.Do(req) if err != nil { fmt.Errorf(Error when try to do the request", "HTTPMethod", http.MethodPost, "Request", req, "Response", response, "url", url, "error", err) } defer response.Body.Close()
DELETE Example
//Create the DELETE request url:= "http://myrestservice/api/clients/2" req, err := http.NewRequest(http.MethodDelete, url ,nil) req.Header.Set("Content-Type", "application/json") if err != nil { fmt.Errorf("Error when try to create the request", "HTTPMethod", http.MethodDelete, "Request", req, "url", url, "error", err) } //Do the request client := &http.Client{} response, err := client.Do(req) defer response.Body.Close() if err != nil { fmt.Errorf("Error when try to do the request", "HTTPMethod", http.MethodDelete, "Request", req, "url", url, "error", err) }
GET Example
The following example will show how to transform the request JSON response in the Golang struct as well.
// Fill the record with the data from the JSON // Transform the body request in the version struct got := models.Client{} //Create the GET request url:= "http://myrestservice/api/clients/2" req, err := http.NewRequest(http.MethodGet, url, nil) req.Header.Set("Content-Type", "application/json") if err != nil { fmt.Errorf("Error when try to create the request", "HTTPMethod", http.MethodGet, "Request", req, "url", url, "error", err) } //Do the request client := &http.Client{} response, err := client.Do(req) defer response.Body.Close() if err != nil { fmt.Errorf("Error when try to do the request", "HTTPMethod", http.MethodGet, "Request", req, "url", url, "error", err) } if 200 != response.StatusCode { fmt.Errorf("The client was not found in the REST Service API", "HTTPMethod", http.MethodGet, "url", url) } //To transform the response in the struct var obj models.Client err = json.NewDecoder(response.Body).Decode(&obj) if err != nil { fmt.Errorf("Error when try to decoder the body response", "HTTPMethod", http.MethodGet, "Request", req, "Response", response, "url", url, "error", err) }