Go in Practice, Second Edition cover
welcome to this free extract from
an online version of the Manning book.
to read more
or

11 Working with external services

 

This chapter covers

  • Making REST requests
  • Detecting request timeouts and resuming downloads
  • Passing errors over HTTP, with additional metadata
  • Parsing JSON, including arbitrary JSON structures
  • Building a versioning system for maintaining different versions of REST APIs
  • Using gRPC as an alternative to architectures like REST

In chapter 10, we explored how to receive data from users in the form of uploads via POST including binary data. We also demonstrated how to return different types of data to the client. When we are ready to be a client ourselves—a consumer of data—we need to know how to deal with different communication protocols and conventions.

This chapter begins with the basics of REST APIs, a cornerstone of the modern internet, and moves to handling cases that don’t go as planned. You’ll look at detecting timeout failures, 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, and you’ll learn about handling JSON responses when you don’t know the structure of the data ahead of time. This is useful when you need to work with poorly defined, undefined, or variable JSON data. When APIs change, they need to be versioned, and you’ll also explore a couple of approaches for versioning your REST APIs.

11.1 Consuming REST APIs as a full-featured client

11.1.1 Using the HTTP client

11.1.2 When faults happen

11.2 Passing and handling errors over HTTP

11.2.1 Generating custom errors

11.2.2 Reading and using custom errors

11.3 Parsing and mapping JSON

11.4 Versioning REST APIs

11.4.1 API version in the URL

11.4.2 API version in content type

11.5 Working with gRPC

Summary