Chapter 8
Talking to REST APIs

Web services that use the representational state transfer (REST) format provide a flexible and agnostic way of exchanging data between computers and different programming languages. Many applications and services on the Internet use this clear and concise format to expose their data to other applications. Using Go to interact with REST APIs opens the door to a large number of services that provide many resources for your command-line tools. It allows you to create flexible tools that were previously challenging to develop by integrating information from multiple sources.

In this chapter, you’ll apply the concepts you learned in Chapter 7, Using the Cobra CLI Framework, to design a command-line tool that connects to a REST API using Go’s net/http package. You’ll explore more advanced concepts such as the http.Client and http.Request types to fine-tune specific connection parameters like headers and timeouts, and you’ll use the encoding/json package to parse JSON response data.

You need access to a REST API to explore how to build a CLI that works with one. You could use one of the many web services available on the Internet. But because these services can change, there’s no guarantee they’ll be available by the time you’re reading this book. Instead, you’ll design and develop your own REST API server so you have a local server to test. This is also a good opportunity to explore the concepts of dealing with network and HTTP requests using Go. You’ll apply the same concepts to develop the command-line client in Developing the Initial Client for the REST API, later in this chapter.

Finally, you’ll use several testing techniques to test your API server as well as your command-line client application, including local tests, simulated responses, mock servers, and integration tests.

Let’s begin by developing the REST API server.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.149.29.112