Viktor Sinelnikov
Viktor Sinelnikov

Reputation: 1631

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.

Upvotes: 5

Views: 6979

Answers (1)

Viktor Sinelnikov
Viktor Sinelnikov

Reputation: 1631

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)

Upvotes: 13

Related Questions