ihsanbal/LoggingInterceptor

header add Twice Error

jinyulei0710 opened this issue · 5 comments

when I use this interceptor to log,The same Header showed twice.

Headers headers = request.headers();
Set names = headers.names();
Iterator iterator = names.iterator();
Request.Builder requestBuilder = request.newBuilder();
for (String key : headerMap.keySet()) {
String value = headerMap.get(key);
requestBuilder.addHeader(key, value);
}
while (iterator.hasNext()) {
String name = iterator.next();
requestBuilder.addHeader(name, headers.get(name));
}

@ihsanbal why you add header twice?

Same to me. Headers are duplicating.

I had fallen back to 2.0.2

    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        HashMap<String, String> headerMap = builder.getHeaders();
        if (headerMap.size() > 0) {
            Headers headers = request.headers();
            Set<String> names = headers.names();
            Iterator<String> iterator = names.iterator();
            Request.Builder requestBuilder = request.newBuilder();
            for (String key : headerMap.keySet()) {
                String value = headerMap.get(key);
                requestBuilder.addHeader(key, value);
            }
            while (iterator.hasNext()) {
                String name = iterator.next();
                requestBuilder.addHeader(name, headers.get(name));
            }
            request = requestBuilder.build();
}```

I am going to solve this problem ASAP. Thanks for reporting.