Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HttpLoggingInterceptor decompresses gzip response body but not request body #6982

Closed
ashraf-atef opened this issue Jan 4, 2022 · 5 comments
Labels
bug Bug in existing code
Milestone

Comments

@ashraf-atef
Copy link

I used the answer mentioned here compress the request body but after adding this interceptor the logging interceptor didn't print the request body and the compressed text is so strange like this:
Original request body: {"data":{"lng":"-122.0840926","lat":"37.4219524"},"device_id":"XXXXX"}
Compressed request body: ���������������9
� �����ڈ�h.�\� �$�x���L�0��D��x�������� =���ኤӤ��r�Z*w�{D�A��J�ՒNX?��r�Q������

@ashraf-atef ashraf-atef added the bug Bug in existing code label Jan 4, 2022
@JakeWharton
Copy link
Member

That looks like gzipped content. Are you saying it's a bug that the interceptor didn't detect non-text content and omit it?

@ashraf-atef
Copy link
Author

@JakeWharton I was thinking that is a wrong gzip text because I was comparing it with online tools gzip the test like this.

The missing thing is the logging http interceptor doesn't log the GZIP request body.

@yschimke
Copy link
Collaborator

yschimke commented Jan 5, 2022

Yep - that seems like a bug. Both request and response body are checked with bodyHasUnknownEncoding, which allows gzip to pass. But only the response is decompressed.

https://github.com/square/okhttp/blob/master/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt#L195

https://github.com/square/okhttp/blob/master/okhttp-logging-interceptor/src/main/kotlin/okhttp3/logging/HttpLoggingInterceptor.kt#L244

@yschimke yschimke changed the title Gzip interceptor generate a strange text HttpLoggingInterceptor decompresses gzip response body but not request body Jan 5, 2022
@yschimke
Copy link
Collaborator

Having looked at implementing this, which is reasonably simple, I realised this line "Compressed request body" comes from your own logging, not the interceptor.

If you are blocked on this, you should just add the following before you log

        if ("gzip".equals(headers["Content-Encoding"], ignoreCase = true)) {
          gzippedLength = buffer.size
          GzipSource(buffer.clone()).use { gzippedResponseBody ->
            buffer = Buffer()
            buffer.writeAll(gzippedResponseBody)
          }
        }

The fix looks reasonably simple https://github.com/square/okhttp/pull/7013/files, and maybe we should add the gzip function to make this simpler.

Not sure whether we want to land this...

@yschimke
Copy link
Collaborator

@yschimke yschimke added this to the 5.0 milestone Jan 29, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug in existing code
Projects
None yet
Development

No branches or pull requests

3 participants