Closed puresprout closed 2 months ago
I suspect you are observing the request headers via an application interceptor, not a network interceptor.
Observe the application’s original intent. Unconcerned with OkHttp-injected headers like If-None-Match.
This should be supported, and the tests cover this case.
Generally, it's not documented because it should be following the HTTP caching specs. You shouldn't need to change your client after setting the cache.
I confirmed with the following test against https://httpbin.org/etag/v1
@Test
@Flaky
fun conditionalCacheHit() {
client =
client.newBuilder()
.cache(cache)
.addNetworkInterceptor {
println("request: " + it.request().headers)
it.proceed(it.request()).also {
println("response: " + it.headers)
}
}
.build()
// Store a response in the cache.
val request = Request("https://httpbin.org/etag/v1".toHttpUrl())
executeSynchronously(request)
.assertCode(200)
// Hit that stored response. It's different, but Vary says it doesn't matter.
Thread.sleep(10) // Make sure the timestamps are unique.
val cacheHit =
executeSynchronously(
request,
)
// Check the merged response. The request is the application's original request.
cacheHit.assertCode(200)
.assertRequestUrl(request.url)
.assertRequestHeader("If-None-Match") // No If-None-Match on the user's request.
// Check the cached response. Its request contains only the saved Vary headers.
cacheHit.cacheResponse()
.assertCode(200)
.assertHeader("ETag", "v1")
.assertRequestUrl(request.url)
.assertRequestHeader("If-None-Match") // This wasn't present in the original request.
// Check the network response. It has the caller's request, plus some caching headers.
cacheHit.networkResponse()
.assertCode(304)
.assertRequestHeader("If-None-Match", "v1") // If-None-Match in the validation request.
}
Please reopen with a reproducible test case.
etag does not work in okhttp 4.12 version It also does not work in the latest alpha.14 version
In server-side implementations, etag works fine.
In other words, the server implementation is completely problem-free.
But on Android
I have structured my app on Android like this:
With the above settings, the actual cache file is created and has contents.
...cabe.0 file content
...cabe.1 file content
logcat
Is etag not working due to a bug in okhttp? Or am I using okhttp incorrectly? If so, please let me know how to use it correctly. There doesn't seem to be any documentation on how to use etag in the okhttp documentation.
Thanks.