title: How to retrieve cookie from response retrofit, okhttp?
body: <p>I am trying to retrieve Cookies values from API response for maintaining the backend session by setting cookie value to new API call in case the APP is closed :</p>
  
  <p>The response to API call from <strong>PostMan RestClient</strong>:
  <a href="https://i.stack.imgur.com/SZzCt.png" rel="noreferrer"><img src="https://i.stack.imgur.com/SZzCt.png" alt="enter image description here"></a></p>
  
  <p><strong>RetrofitClient.java</strong></p>
  
  <pre><code>public static Retrofit getClient() {
  
      if (checkClient()) {
          return getRetrofit();
      }
  
      HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
      interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
      CookieHandler cookieHandler = new CookieManager();
  
      okhttp3.OkHttpClient client = new okhttp3.OkHttpClient.Builder().addNetworkInterceptor(interceptor)
              .cookieJar(new JavaNetCookieJar(cookieHandler))
              .connectTimeout(10, TimeUnit.SECONDS)
              .writeTimeout(10, TimeUnit.SECONDS)
              .readTimeout(30, TimeUnit.SECONDS)
              .build();
  
      Gson gson = new GsonBuilder()
              .setLenient()
              .create();
  
      retrofit = new Retrofit.Builder()
              .baseUrl(BASE_URL)
              .addConverterFactory(GsonConverterFactory.create(gson))
              .client(client)
              .build();
      return retrofit;
  }
  </code></pre>
  
  <p><strong>API Call</strong></p>
  
  <pre><code>private void userLogin(String username, String password) {
      Retrofit retrofit = RetrofitClient.getClient();
  
      final LoginServices loginServices = retrofit.create(LoginServices.class);
      Call&lt;LoginResponse&gt; call = loginServices.userLogin(username, password);
  
      call.enqueue(new Callback&lt;LoginResponse&gt;() {
          @Override
          public void onResponse(Call&lt;LoginResponse&gt; call, Response&lt;LoginResponse&gt; response) {
              Log.d("Response code:", "===========" + response.code());
  
              Log.d("==Cookie==1===", "==="+response.raw().request().headers().get("Cookie"));
              Log.d("==Cookie==2==", "==="+response.headers().get("Cookie"));
              Log.d("==Content-Type====", "==="+response.headers().get("Content-Type"));
  
          }
  
          @Override
          public void onFailure(Call&lt;LoginResponse&gt; call, Throwable t) {
              Log.e("TAG", "=======onFailure: " + t.toString());
              t.printStackTrace();
              // Log error here since request failed
          }
      });
  }
  </code></pre>
  
  <p><strong>Logcat:</strong></p>
  
  <pre><code>Response code:: ===========200
  ==Cookie==1===: ===null
  ==Cookie==1===: ===null
  ==Content-Type====: ===application/json
  </code></pre>
  
  <p>I also tried other various methods they didn't worked for me. Please Help!</p>