Viewing a Single Item

At this point, our application can only query the to-do REST API for all the items. Let’s add the ability to get details for a specific item.

The to-do REST API returns information about a single item by querying the URL /todo/id using the HTTP Get method. The id is a numeric identifier that represents an item from the list. We can find the item id using the list command. When we’re working with an API we’re unfamiliar with, we consult the REST API documentation to understand how to make queries for particular resources that we’re interested in.

Updating the cmd/client.go file

To obtain a single item from the REST API, first we add a new function to the cmd/client.go file. This function wraps the getItems() function similarly to the getAll() function we developed for the list command, but providing the proper URL endpoint to only query one item. We name this function getOne(). It takes as input the apiRoot and the integer ID that represents the item identifier. It returns an instance of the item type and an error:

Get hands-on with 1400+ tech skills courses.