We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Create a new Interceptor with the code pasted below and add it as an interceptor in Okhttp library builder.
internal class GzipRequestInterceptor : Interceptor { @Throws(IOException::class) override fun intercept(chain: Interceptor.Chain): Response { val originalRequest = chain.request() if (originalRequest.body() == null || originalRequest.header("Content-Encoding") != null) { return chain.proceed(originalRequest) } val compressedRequest = originalRequest.newBuilder() .header("Content-Encoding", "gzip") .method(originalRequest.method(), gzip(originalRequest.body())) .build() return chain.proceed(compressedRequest) } private fun gzip(body: RequestBody?): RequestBody = object : RequestBody() { override fun contentType(): MediaType? { return body?.contentType() } override fun contentLength(): Long { return -1 // We don't know the compressed length in advance! } @Throws(IOException::class) override fun writeTo(sink: BufferedSink) { val gzipSink = Okio.buffer(GzipSink(sink)) body?.writeTo(gzipSink) gzipSink.close() } } }
Add GZip request compression Interceptors rewriting-request
The text was updated successfully, but these errors were encountered:
I need this too
Sorry, something went wrong.
No branches or pull requests
Create a new Interceptor with the code pasted below and add it as an interceptor in Okhttp library builder.
Code
References
Add GZip request compression
Interceptors rewriting-request
The text was updated successfully, but these errors were encountered: