2

There are already some code snippets in the net, which show how it should work. For example this one.

My code doesn't work; and I'm also searching for a version to send the Cookie header dynamically (I mean only on specific requests - the login doesn't require it).

So my code:

HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
logging.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
httpClient.addInterceptor(logging);
// header
// httpClient.addInterceptor(chain -> cookie != null ? chain.proceed(chain.request().newBuilder().addHeader("Set-Cookie", cookie).build()) : chain.proceed(chain.request()));
httpClient.addInterceptor(chain -> chain.proceed(chain.request().newBuilder().addheader("Cookie", cookie).build()));

Retrofit retrofit = new Retrofit.Builder().baseUrl(url)
         .addConverterFactory(GsonConverterFactory.create())
         .client(httpClient.build())
         .build();

I'm not getting this header in my log!

--> POST [link] http/1.1
Content-Type: application/json; charset=UTF-8
Content-Length: 125

{// response object

My requests itself are retrofit-base; just an interface with parameters... nothing special where I could maybe change something I shouldn't to fix this error.

Has anybody a idea? And how can I make it dynamic? With the comment line?

I read, that retrofit supported the @Header Annotation also as parameter. Support canceled?

Community
  • 1
  • 1
rala
  • 895
  • 2
  • 18
  • 43
  • Can you confirm that your cookie value is not null/empty? – Alex Townsend Apr 07 '16 at 18:59
  • yes; if it is, is this the reason for no header? | on first request it is empty, but not on second (which is waiting for first one) – rala Apr 07 '16 at 19:00
  • If it's empty I believe it should still appear on the logging interceptor output. Null would cause an exception. Are you expecting it to be set with the first request? – Alex Townsend Apr 07 '16 at 19:06
  • You should also be able to set headers in the interface declaration, like so: `Call callOperation(@Header("Cookie") String cookie...)` – Alex Townsend Apr 07 '16 at 19:09
  • actually not (only for test reasons - compare the two lines beyond `// header`) – rala Apr 07 '16 at 19:11
  • thank you for the info, but it didn't work either – rala Apr 07 '16 at 19:12

1 Answers1

1

at retrofit 2.1.+ the @Header annotation works fine

rala
  • 895
  • 2
  • 18
  • 43