Teasel
Teasel

Reputation: 1340

Retrofit does not send header


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

Upvotes: 0

Views: 2710

Answers (1)

aminner
aminner

Reputation: 359

// 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

Upvotes: 3

Related Questions