0


I'm currently working on an Android app and I have a problem, I'm trying to send a header in my request with retrofit but when I check on my server with PHP it looks like the header does not even exists.
Here is my code:
Android

@Headers("SECRET_KEY: QWERTZUIOP")
@GET("{TableName}/")
Call<List<Data>> cl_getAllFromTable(@Path("TableName") String TableName);


PHP Server

$secret_key = $_SERVER['HTTP_SECRET_KEY'];


I'd be glad if someone could help. Thanks in advance.
Teasel

Teasel
  • 1,330
  • 4
  • 18
  • 25
  • http://stackoverflow.com/questions/32963394/how-to-use-interceptor-to-add-headers-in-retrofit-2-0 You have to intercept and add the header manually. – aminner Jun 06 '16 at 17:20
  • I need to send the header from my app, is that possible ? Can you tell me more please. – Teasel Jun 06 '16 at 17:39
  • http://stackoverflow.com/questions/29884967/how-to-dynamically-set-headers-in-retrofit-android The accepted answer gives an explanation and several options! – aminner Jun 06 '16 at 18:36
  • 1
    Why is "SECRET_KEY" and "HTTP_SECRET_KEY" different? – Justin Slade Jul 07 '16 at 05:08

1 Answers1

3
// Define the interceptor, add authentication headers
Interceptor interceptor = new Interceptor() {
       @Override
       public okhttp3.Response intercept(Chain chain) throws IOException {
          Request newRequest = chain.request().newBuilder().addHeader("User-Agent", "Retrofit-Sample-App").build();
          return chain.proceed(newRequest);
       }
};

// Add the interceptor to OkHttpClient
OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.interceptors().add(interceptor);
OkHttpClient client = builder.build();

// Set the custom client when building adapter
Retrofit retrofit = new Retrofit.Builder()
       .baseUrl("https://api.github.com")
       .addConverterFactory(GsonConverterFactory.create())
       .client(client)
       .build();

Reference: https://guides.codepath.com/android/Consuming-APIs-with-Retrofit

aminner
  • 359
  • 3
  • 10