title: Retrofit2: 504 Unsatisfiable Request (only-if-cached)
body: <p>I am trying to cache a network response using Retrofit2's caching. However, when I try to use the cached response, I get the error in the title. Here is what I have:</p>
  <pre><code>public OkHttpClient provideClient(@ApplicationContext Context context) {
      int cacheSize = 10 * 1024 * 1024; // 10 MB
      Cache cache = new Cache(context.getCacheDir(), cacheSize);
      int maxStale = 60 * 60 * 24 * 7; // 1 Week
  
      return new OkHttpClient.Builder()
              .cache(cache)
              .addInterceptor(chain -&gt; {
                  Request request = chain.request();
                  if (isNetworkAvailable()) {
                      request = request.newBuilder()
                              .removeHeader(&quot;Pragma&quot;)
                              .header(&quot;Cache-Control&quot;, &quot;private, only-if-cached, max-stale=&quot; + maxStale)
                              .build();
                  } else {
                      request = request.newBuilder()
                              .removeHeader(&quot;Pragma&quot;)
                              .header(&quot;Cache-Control&quot;, &quot;private, max-age=&quot; + 30)
                              .build();
                  }
                  return chain.proceed(request);
              })
              .build();
  }
  </code></pre>
  <p>I've read countless SO posts, but none of them seem to help me. Perhaps what is different for me is that I need to cache a <code>POST</code> response. If my understanding is correct, you can still cache a <code>POST</code> response as long as it has the appropriate <code>Cache-Control</code> headers. This is what the relevant part of the header looks like:</p>
  <pre><code>cache-control: no-cache, private
  pragma: no-cache
  ...
  </code></pre>
  <p>Now according to <a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Cache-Control" rel="nofollow noreferrer">this guide</a>,</p>
  <blockquote>
  <p>The no-cache response directive indicates that the response can be
  stored in caches, but the response must be validated with the origin
  server before each reuse, even when the cache is disconnected from the
  origin server.</p>
  </blockquote>
  <p>so it should still be possible to cache the response? Or is it impossible?</p>