If this is just a one-time insertion of the header on the Request , then it is mandatory: request.newBuilder().addHeader("header-name", "value").build();
If you want to do this for all Request in OkHttpClient , use an interceptor:
private static final class AddHeaderInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request(); request = request.newBuilder().addHeader("header-name", "value").build(); return chain.proceed(request); } }
Regarding what newBuilder () does, read the source. :) https://github.com/square/okhttp/blob/0ac2471d0678dfa9d535fbb13a546134dc2b3089/okhttp/src/main/java/com/squareup/okhttp/Reest.quest
source share