
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.