Adding an Item

Overview

So far, our to-do REST API client is able to get all the items from the list and view details about specific items. Let’s include the ability to add new items to the list so users can track their new tasks.

To add new tasks to the to-do list using our REST API, the client must send an HTTP POST request to the /todo endpoint containing the task as a JSON payload. As usual, we obtain this information from the API’s documentation to understand their requirements.

Updating the cmd/client.go file

Let’s define the logic to send the HTTP POST requests in the cmd/client.go file. We edit this file and update the import section by including two new dependencies:

  • The bytes package to use a buffer of bytes as the content body.
  • The io package to use the io.Reader interface.

Get hands-on with 1400+ tech skills courses.