Skip to content
tezvyn:
Menu

OkHttp Interceptors: Middleware for Your Network Stack

MediumHow cards are made

OkHttp Interceptors act as middleware for your network stack, letting you observe, modify, or short-circuit requests and responses. Use them to automatically add auth tokens or log traffic. The main footgun is order: application interceptors run once per call.

Why it exists

Modern apps have cross-cutting network concerns like authentication, logging, and caching. Instead of cluttering every API call site with this repetitive logic, OkHttp needed a clean, centralized way to apply these operations to every request. Interceptors provide this single, chainable point of entry.

The mental model

Think of interceptors as a series of checkpoints on a highway. A request starts at one end, and at each checkpoint (interceptor), you can inspect it, modify it, or even turn it around. The request must be passed to the next checkpoint by calling chain.proceed(). The response then travels back through the same checkpoints in reverse order, giving you a chance to inspect or modify it, too.

How it works

An interceptor implements a single method, intercept(Chain chain). Inside, you get the request, optionally modify it, and crucially, call chain.proceed(request) to pass it to the next interceptor. There are two types, and their order is critical.

Application Interceptors: Added via addInterceptor(). These run first. They see the raw request you created and are best for high-level logic like adding headers or logging the intended call. They are unaware of network-level details like redirects or retries; they only run once for the initial call.

Network Interceptors: Added via addNetworkInterceptor(). These run last, just before the request goes over the network. They can see the fully-formed request, including any changes made by OkHttp itself (like headers for compression or handling redirects). They are ideal for monitoring the actual bytes going over the wire or measuring latency. They may run multiple times if a request is retried or redirected.

When to use it

Use interceptors for app-wide concerns. Three common uses: first, adding an Authorization header to every request; second, logging request and response data for debugging (like OkHttp's own HttpLoggingInterceptor); third, implementing a global error handling strategy, such as refreshing an expired token when you receive a 401 Unauthorized response.

When not to use it

Avoid interceptors for logic specific to a single API call. If only one endpoint needs a special header, add it when building that specific Request object. Overusing interceptors for endpoint-specific logic makes the system hard to reason about. Also, do not perform long-running, blocking work inside an interceptor, as it will stall the entire network call chain.

One canonical example

An AuthInterceptor is the classic example. It intercepts every request, creates a new request by adding an Authorization header with a bearer token, and then proceeds with this new, authenticated request. This cleanly separates authentication logic from the code that makes the API calls, which now doesn't need to know or care about how authentication is handled.

Interview question

Which OkHttp interceptor type is best for observing the final request sent over the network, including any redirects or retries?

  • a.Neither, as OkHttp handles redirects and retries internally without exposing them to interceptors.
  • b.Application Interceptors, as they run first and see the initial request.
  • c.Network Interceptors, as they run just before the network and may execute multiple times.Correct
  • d.Both types, as they both have access to the Request object.
Why?

Network Interceptors run last, just before the request goes over the network, and may run multiple times for retries or redirects, allowing observation of the final request. Application Interceptors run only once per call and are unaware of these network-level details.

Just read this? Test yourself on what you have been reading.

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Every open role lists the topics its interview covers, so you can prepare for the real thing rather than guessing.

See open roles