Chapter 8 Talking to REST APIs

Powerful Command-Line Applications in Go — by Ricardo Gerardi (84 / 127)

The Pragmatic Programmers
The Pragmatic Programmers

--

👈 Wrapping Up | TOC | Developing a REST API Server 👉

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…

--

--

The Pragmatic Programmers
The Pragmatic Programmers

We create timely, practical books and learning resources on classic and cutting-edge topics to help you practice your craft and accelerate your career.