Reputation: 723
I want to make a request with two headers in an Android app, the first one has a parameter. I tried this:
@Headers("Content-Type: application/json")
@GET("/ebuzon/client/show")
Call<Client> showClient(@Header("Authorization") String token);
call = api.showClient(" Bearer " +MySharedUser.getInstance().getToken());
401 UNATHORIZED
Edited: this should work, see @Robert answer. My problem was also updating token
@GET("/ebuzon/client/show")
Call<Client> showClient(@Header("Authorization") String token,
@Header("Content-Type") String appJson);
call = api.showClient(" Bearer" +MySharedUser.getInstance().getToken(), "application/json");
401 UNATHORIZED
@Headers({"Authorization: Bearer {token}","Content-Type: application/json"})
@GET("/ebuzon/client/show")
Call<Client> showClient(@Path("token") String token);
IllegalArgumentException: URL "/ebuzon/client/show" does not contain "{token}". (parameter #1)
I am doing the same in angular like this and it's working
var obj = this.http.get("/ebuzon/client/show",
{headers: new HttpHeaders().set("Authorization", " Bearer "+ this.token).append("Content-Type","application/json")})
.map(res => {
console.log(res);
return res
})
.catch(err => {
console.log(err);
return err;
})
return obj;
Thanks for any help.
Upvotes: 0
Views: 2305
Reputation: 12477
Method 1 or 2 should work.
I believe the problem might be on the way you construct the value.
(" Bearer" +MySharedUser.getInstance().getToken());
This will result in something like
" Bearer{SOME_TOKEN}"
When in reality, you most likely want it to be something like
"Bearer {SOME_TOKEN}"
Upvotes: 1
Reputation: 228
Maybe this will help you out..Maybe you need to add a header in your request..
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request newRequest = chain.request().newBuilder()
.addHeader("Authorization", "Bearer " + token)
.build();
return chain.proceed(newRequest);
}
}).build();
Retrofit retrofit = new Retrofit.Builder()
.client(client)
.baseUrl(/** your url **/)
.addConverterFactory(GsonConverterFactory.create())
.build();
Upvotes: 1