jpotts18 jpotts18 - 2 years ago 143
Java Question

Android Retrofit Parameterized @Headers

I am using OAuth and I need to put the OAuth token in my header every time I make a request. I see the

annotation, but is there a way to make it parameterized so i can pass in at run time?

Here is the concept

@Header({Authorization:'OAuth {var}', api_version={var} })

Can you pass them in at Runtime?

void getUsers(
@Header("Authorization") String auth,
@Header("X-Api-Version") String version,
Callback<User> callback

Answer Source

Besides using @Header parameter, I'd rather use RequestInterceptor to update all your request without changing your interface. Using something like:

RestAdapter.Builder builder = new RestAdapter.Builder()
    .setRequestInterceptor(new RequestInterceptor() {
        public void intercept(RequestFacade request) {
            request.addHeader("Accept", "application/json;versions=1");
            if (isUserLoggedIn()) {
                request.addHeader("Authorization", getToken());
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download