0

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.

Pablo R.
  • 711
  • 2
  • 10
  • 31
  • please try this way -https://stackoverflow.com/questions/29884967/how-to-dynamically-set-headers-in-retrofit-android – Adil Feb 01 '18 at 10:35

2 Answers2

1

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();
DPE
  • 218
  • 5
  • 15
1

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}"
Robert Estivill
  • 12,369
  • 8
  • 43
  • 64