Chapter 8. Working with web services


This chapter covers

  • Making REST requests
  • Detecting timeouts and resuming downloads
  • Passing errors over HTTP
  • Parsing JSON, including arbitrary JSON structures
  • Versioning REST APIs

REST APIs are a cornerstone of the modern internet. They enable cloud computing, have been a pillar in the DevOps and automation movements, and set up client-side web development, among other things. They’re one of the great enablers on the internet.

Although plenty of tutorials about creating and consuming simple APIs are available, what happens when things don’t go as planned? The internet was designed to be fault-tolerant. API requests and servers need to enable that fault tolerance to work.

This chapter starts with the basics of REST APIs and quickly moves on to handling cases that don’t go as planned. You’ll look at detecting timeout failures, including those that Go doesn’t formally flag as timeouts. You’ll also look at resuming file transfers when timeouts happen, and you’ll learn how to pass errors between an API endpoint and a requesting client.

Many APIs pass information as JSON. After a quick look at how JSON parsing works in Go, you’ll learn about handling JSON structures when you don’t know the structure of the data ahead of time. This is useful when you need to work with poorly defined or undefined JSON data.

8.1. Using REST APIs

8.2. Passing and handling errors over HTTP

8.3. Parsing and mapping JSON

8.4. Versioning REST APIs

8.5. Summary