mirror of
https://github.com/square/okhttp.git
synced 2025-11-27 18:21:14 +03:00
Remove the need for an explicit Content-Length header to log response body.
This allows chunked responses with no specified length to be logged and have their size reported correctly.
This commit is contained in:
@@ -157,6 +157,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
||||
logger.log(headers.name(i) + ": " + headers.value(i));
|
||||
}
|
||||
|
||||
String endMessage = "--> END " + request.method();
|
||||
if (logBody && hasRequestBody) {
|
||||
Buffer buffer = new Buffer();
|
||||
requestBody.writeTo(buffer);
|
||||
@@ -169,10 +170,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
||||
|
||||
logger.log("");
|
||||
logger.log(buffer.readString(charset));
|
||||
}
|
||||
|
||||
String endMessage = "--> END " + request.method();
|
||||
if (logBody && hasRequestBody) {
|
||||
endMessage += " (" + requestBody.contentLength() + "-byte body)";
|
||||
}
|
||||
logger.log(endMessage);
|
||||
@@ -193,6 +191,7 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
||||
logger.log(headers.name(i) + ": " + headers.value(i));
|
||||
}
|
||||
|
||||
String endMessage = "<-- END HTTP";
|
||||
if (logBody) {
|
||||
BufferedSource source = responseBody.source();
|
||||
source.request(Long.MAX_VALUE); // Buffer the entire body.
|
||||
@@ -204,15 +203,12 @@ public final class HttpLoggingInterceptor implements Interceptor {
|
||||
charset = contentType.charset(UTF8);
|
||||
}
|
||||
|
||||
if (responseBody.contentLength() > 0) {
|
||||
if (responseBody.contentLength() != 0) {
|
||||
logger.log("");
|
||||
logger.log(buffer.clone().readString(charset));
|
||||
}
|
||||
}
|
||||
|
||||
String endMessage = "<-- END HTTP";
|
||||
if (logBody) {
|
||||
endMessage += " (" + responseBody.contentLength() + "-byte body)";
|
||||
endMessage += " (" + buffer.size() + "-byte body)";
|
||||
}
|
||||
logger.log(endMessage);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user