Reputation: 3450
I'm trying to do an @GET request with Retrofit2. I'm just have a good request to take the token making a login, but now when I'm trying to do this call I don't get anything.
This is the error shows by an interceptor:
╭--- cURL (http://192.168.1.107/project-task)
curl -X GET -H "Host: 192.168.1.107" -H "Connection: Keep-Alive" -H "Accept-Encoding: gzip" -H "User-Agent: okhttp/3.4.1" --compressed http://192.168.1.107/project-task
╰--- (copy and paste the above line to a terminal)
--> GET http://192.168.1.107/project-task http/1.1
Host: 192.168.1.107
Connection: Keep-Alive
Accept-Encoding: gzip
User-Agent: okhttp/3.4.1
--> END GET
<-- 401 Unauthorized http://192.168.1.107/project-task (167ms)
X-Powered-By: Sails <sailsjs.org>
WWW-Authenticate: Bearer realm="Users"
set-cookie: sails.sid=s%3Aw3NbP_fcx-rnmnDaJSIUcs_ZgQB5ar5B.dZogKwU4nOlmmplnjqMZAUbL4eshjLITmBpkiZLdNkU; Path=/; HttpOnly
Date: Mon, 05 Sep 2016 11:25:09 GMT
Connection: keep-alive
Transfer-Encoding: chunked
<-- END HTTP
The call that I make with the GET is:
@GET("project-task")
Call<RootContainer> getChargeAccountsNTasks(@Header("Authorization") String token);
The implementation used with this is the next code, but crashs con the call object, and I think that it is because the token is not added to the header.
public class DataApiRequestImpl implements DataApiRequest {
private GetDataRequestEntity getDataRequestEntity;
private RetrofitApi retrofitApi;
@Inject
public DataApiRequestImpl(
GetDataRequestEntity getDataRequestEntity,
RetrofitApi retrofitApi) {
this.getDataRequestEntity = getDataRequestEntity;
this.retrofitApi = retrofitApi;
}
@Override
public RootContainer getChargeAccountsNTasks(String token) throws Exception {
RootContainer data = null;
Call<RootContainer> call = retrofitApi.getChargeAccountsNTasks(token);
Response<RootContainer> response = call.execute();
data = response.body();
return data;
}
}
How I can know if the token is being added to the header?
EDIT: Added UserModule, which has the initialization methods to do calls.
@Module
public class UserModule {
public UserModule() {}
@Provides
public Retrofit retrofit() {
OkHttpClient.Builder okHttpClientBuilder = new OkHttpClient.Builder();
if (BuildConfig.DEBUG) {
CurlLoggingInterceptor curlLoggingInterceptor = new CurlLoggingInterceptor();
okHttpClientBuilder.addNetworkInterceptor(curlLoggingInterceptor);
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
interceptor.setLevel(HttpLoggingInterceptor.Level.HEADERS);
okHttpClientBuilder.addNetworkInterceptor(interceptor);
}
return new Retrofit.Builder()
.baseUrl(BuildConfig.API_URL)
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClientBuilder.build())
.build();
}
@Provides
public RetrofitApi userRetrofitApi(Retrofit retrofit) {
return retrofit.create(RetrofitApi.class);
}
@Provides
@PerActivity
LoginUser provideLoginUserInteractor(LoginUserInteractor interactor) {
return interactor;
}
@Provides
@PerActivity
LogoutUser provideLogoutUserInteractor(LogoutUserInteractor interactor) {
return interactor;
}
@Provides
public LoginRequestEntity loginRequestEntity() {
return new LoginRequestEntity();
}
}
Upvotes: 0
Views: 551
Reputation: 2154
You are passing Authorization as a parameter not Header. I will show you the example code which I've done before using OkHttp:
OkHttpClient httpClient = getOkHttpClient();
httpClient.interceptors().clear();
httpClient.interceptors().add(new Interceptor() {
@Override
public Response intercept(Interceptor.Chain chain) throws IOException {
Request original = chain.request();
// Set authorization token here
Request.Builder requestBuilder = original.newBuilder()
.header("Authorization", token)
.header("Connection", "close")
.method(original.method(), original.body());
Request request = requestBuilder.build();
return chain.proceed(request);
}
});
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
// Assign retorfitAPI like this
RetrofitApi retrofitApi = retrofit.create(serviceClass);
And then call the API.
Upvotes: 2