This chapter shows how to bring external HTTP services into a Rust web application and why that matters in real projects. It starts from the idea that many services need to talk to third-party APIs or other microservices, then walks through practical examples such as URL shortening, address verification, stock lookup, messaging, and email workflows. The main goal is to show how these calls affect the codebase, especially when they are introduced into route handlers that already manage database work and request processing.
A large part of the chapter focuses on choosing the right HTTP client and understanding its tradeoffs. It compares low-level and higher-level approaches, including Hyper and Reqwest, and recommends sticking with a client that fits the runtime already used by the application. The chapter also stresses the importance of testing API calls in a small helper project before integrating them into the main codebase, because Rust’s ownership model and type system can make mistakes surface early. Once a working request is found, the next steps are handling authentication, inspecting documentation carefully, and learning how to interpret both successful responses and failures from the external service.
The chapter then builds a complete integration around a profanity-checking API. It introduces typed response structures, custom error types, and logging so that internal failures can be distinguished from client-facing responses. After that, the profanity check is extracted into a reusable helper and applied to question and answer handlers, with content being censored before storage. Finally, the chapter explores more advanced concerns such as timeouts, retries with exponential backoff, and running multiple HTTP requests concurrently or in parallel using Tokio, reinforcing the idea that Rust’s strong typing and ecosystem help make external integrations safer and more maintainable.
An application like Postman lets you collect and save HTTP requests so you can easily refire them and therefore quickly test endpoints and workflows.
The documentation for the send method of the Reqwest crate, which states when this method throws an error
The documentation for error_for_status seems to return the possible API error we want.
We have to handle two errors in our flow: an internal crate error and nonsuccess (4xx,5xx) HTTP responses from the web service.
The online Transform tool can create Rust structs out of a given JSON.
Summary
- Choosing an HTTP client depends partly on the runtime you chose for your project.
- You can choose from various levels of abstraction when adding an HTTP client, depending on your needs.
- Going with a widely used crate gives you the advantage of a richer ecosystem around the crate and more help on the internet.
- Error handling is crucial when implementing HTTP calls to other services.
- You don’t want to expose internal errors to clients.
- You also don’t want to lose details when logging errors.
- Rust gives you the option of handling errors internally and externally at the same time with different details.
- Creating structs for responses and errors from your third-party API service helps future developers but also makes your codebase easier to study and extend, and prevents possible mistakes in the future.
- Adding default retries of failed HTTP calls helps prevent the user from sending the same request multiple times a second.
- The Tokio runtime exposes methods and macros to bundle futures to be able to work concurrently or in parallel (via join! or spawn).
Rust Web Development ebook for free