Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Authorization header with Retrofit2 + RxJava

I want to perform request using Retrofit2 and RxJava

public static Observable<Post> getPostsAround(Location location, int offset, int limit) {
    if(api==null) {
        new RestService();  //initialize API in constructor
    }
    return api.getPostsAround(location.getLatitude(),location.getLongitude(),offset,limit)
            .flatMapIterable(posts -> posts);   //transform Observable<List<Post>> to Observable<Post> which emits posts onNext
}

I tried @Headers("Authorization: code) annotation, but I don't know how to change "code" in runtime.

like image 720
Viktor Sinelnikov Avatar asked Jul 11 '16 20:07

Viktor Sinelnikov


1 Answers

I have found an answer: A request Header can be updated dynamically using the @Header annotation. A corresponding parameter must be provided to the @Header. If the value is null, the header will be omitted. Otherwise, toString will be called on the value, and the result used.

@GET("user")
Call<User> getUser(@Header("Authorization") String authorization)
like image 71
Viktor Sinelnikov Avatar answered Sep 24 '22 14:09

Viktor Sinelnikov