Interacting with REST APIs
Often, if a company or user has already created an API, the command-line application will send requests to either the REST API or the gRPC endpoints. Let’s first talk about using REST API endpoints. It is important to understand the net/http
package. It’s quite a large package with many types, methods, and functions, many of which are used for development on the server side. In this context, the command-line application will be the client of the API, so we won’t discuss each in detail. We’ll go into a few basic use cases from the client side though.
Get request
Let’s revisit the code from Chapter 3, Building an Audio Metadata CLI. Within the Run
command of the CLI command code, found in the /cmd/cli/command/get.go
file, is a snippet of code that calls the corresponding API request endpoint using the GET
method:
params := "id=" + url.QueryEscape(cmd.id) path := fmt.Sprintf("http://localhost/request...