Skip to content

Instantly share code, notes, and snippets.

Last active July 20, 2023 19:01
  • Star 97 You must be signed in to star a gist
  • Fork 15 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
This OkHttp application interceptor will replace the destination hostname in the request URL.
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
/** An interceptor that allows runtime changes to the URL hostname. */
public final class HostSelectionInterceptor implements Interceptor {
private volatile String host;
public void setHost(String host) { = host;
@Override public okhttp3.Response intercept(Chain chain) throws IOException {
Request request = chain.request();
String host =;
if (host != null) {
HttpUrl newUrl = request.url().newBuilder()
request = request.newBuilder()
return chain.proceed(request);
public static void main(String[] args) throws Exception {
HostSelectionInterceptor interceptor = new HostSelectionInterceptor();
OkHttpClient okHttpClient = new OkHttpClient.Builder()
Request request = new Request.Builder()
okhttp3.Call call1 = okHttpClient.newCall(request);
okhttp3.Response response1 = call1.execute();
System.out.println("RESPONSE FROM: " + response1.request().url());
okhttp3.Call call2 = okHttpClient.newCall(request);
okhttp3.Response response2 = call2.execute();
System.out.println("RESPONSE FROM: " + response2.request().url());
Copy link

This seems to be working for me.

class ChangeableBaseUrlInterceptor : Interceptor {
    @Volatile private var host: HttpUrl? = null

    fun setHost(url: String) { = HttpUrl.parse(url)

    override fun intercept(chain: Interceptor.Chain): Response {
        val newRequest = host?.let {
            val newUrl = chain.request().url().newBuilder()

            return@let chain.request().newBuilder()

        return chain.proceed(newRequest)

Copy link

This solution will affect @get (full path) @post (full path) and @url so that they can not work,try my solution:

Copy link

@JessYanCoding was using this class until I ran into same issue you mentioned. My @url's base url got replaced with intercepted host. Thanks for creating your library.

Copy link

@JessYanCoding Could you elaborate on why this solution won't work? The whole objective is to intercept the request and change the host alone to whatever we want. So how is this affecting @get(full path) ?

Copy link

JessYanCoding commented Mar 24, 2018

@thsaravana You try, @get(full path) will be replaced with intercepted host

Copy link

iBotasky commented Apr 6, 2018

I request two api with different baseUrl at the same time, one of the request's baseUrl is wrong.

                .flatMap { filmsData -> Observable.fromIterable(filmsData.films) }
                        { film ->
                            val filmEntity = FilmEntity(text = film.title, comment = film.originalTitle, date = Date())

                            Log.e("TAG", "put success " + filmEntity.text)
                        { e ->
                            Log.e("TAG", e.message)

                .subscribe({ girl ->
                    Log.e("TAG", "size " + girl.results.size)

The two baseurl are: and
And occur this:
D/OkHttp: <-- 404 Not Found (308ms)
This is wrong because the base url is but not
I think it course by concurrency with the Volatile , how solve it?

Copy link

@iBotasky Because the network request is asynchronous, try

Copy link

I have a good implementation plan, which will be easier to use and solve the problem more thoroughly. Welcome everyone to try

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment