Adding header to all request with Retrofit 2
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When building Android applications, network communication is fundamental, and Retrofit 2 is a prominent library for handling API requests efficiently. Often, there is a need to add headers to every request, such as authentication tokens or special API keys. Retrofit provides several mechanisms to streamline this process. This article delves into the different strategies for adding headers to all requests in Retrofit 2, with detailed technical explanations and examples.
Basics of Retrofit
Retrofit is a type-safe HTTP client for Android and Java developed by Square. It simplifies network communication by allowing developers to define REST API interactions declaratively. The library uses annotations for request configuration and supports mapping responses to Java objects effortlessly.
Here's a simple setup for a Retrofit client:
Adding Headers: Strategies
1. Using @Header Annotation
The most straightforward method to add headers to individual requests is the @Header annotation. However, this approach isn't suitable for headers you need to attach globally.
2. Dynamic Headers with Interceptor
Interceptors enable adding headers that apply globally across every request. Retrofit facilitates this through OkHttp interceptors:
Implementation:
- Create an Interceptor:
- Attach Interceptor to OkHttp Client:
3. Static Headers with @Headers Annotation
For headers that are constant, you can utilize the @Headers annotation:
4. Customizing Headers Conditionally
Sometimes headers depend on runtime conditions. In such cases, interceptors are flexible and can incorporate logic to adjust headers dynamically:
Key Points Summary
| Strategy | Use Case | Pros | Cons |
@Header Annotation | Per-request headers | Simple and inline | Not global |
@Headers Annotation | Static headers | Easy for constants | Can't change dynamically |
| Interceptor | Global or dynamic | Global control, highly flexible | Requires OkHttp setup |
| Dynamic Interceptor with Logic | Condition-based headers | Full flexibility | Increased complexity |
Additional Considerations
Thread Safety and Performance
- Ensure interceptors are thread-safe, especially when modifying requests with mutable data sources.
- Reuse OkHttp clients to enhance performance and avoid unnecessary resource consumption.
Advanced Techniques
Authentication Refresh
If your application handles authorization and requires token refreshing, leveraging an interceptor to catch 401 Unauthorized responses and automatically refresh tokens could significantly streamline error recovery and keep your app sessions alive.
Debugging
Utilizing OkHttp's logging capabilities can aid in debugging network requests, particularly when handling headers. The HttpLoggingInterceptor is valuable in such scenarios.
Conclusion
With Retrofit 2, managing headers on HTTP requests can be as specific or as consistent as needed using annotations and interceptors. While annotations handle static use cases well, interceptors provide the flexibility needed for dynamic and global configurations. By understanding and utilizing these strategies, developers can craft robust, adaptable network layers within their applications.

