25

I'm using Square's Retrofit library for short-lived network calls. There are a few pieces of data that I include as @Query params on every request. Like so:

@GET("/thingOne.php")
void thingOne(
        @Query("app_version") String appVersion,
        @Query("device_type") String deviceType,
        Callback<Map<String,Object>> callback
);

@GET("/thingTwo.php")
void thingTwo(
        @Query("app_version") String appVersion,
        @Query("device_type") String deviceType,
        Callback<Map<String,Object>> callback
);

It's cumbersome to have to define appVersion and deviceType for every single endpoint outlined in the Interface. Is there a way to set a base set of parameters that should be included with every request? Something similar to how we set a common Authorization Header?

RestAdapter restAdapter = new RestAdapter.Builder()
    .setServer("...")
    .setRequestHeaders(new RequestHeaders() {
        @Override
        public List<Header> get() {
            List<Header> headers = new ArrayList<Header>();
                Header authHeader = new Header(
                    "Authorization", "Bearer " + token);
                headers.add(authHeader);
            }
            return headers;
        }
    })
    .build();
this.service = restAdapter.create(ClientInterface.class);
JJD
  • 50,076
  • 60
  • 203
  • 339
Alfie Hanssen
  • 16,964
  • 12
  • 68
  • 74

1 Answers1

54

You can ensure all requests have these query parameters by adding a custom RequestInterceptor to your RestAdapter

RequestInterceptor requestInterceptor = new RequestInterceptor()
{
    @Override
    public void intercept(RequestFacade request) {
        request.addQueryParam("app_version", "Version 1.x");
        request.addQueryParam("device_type", "Samsung S4");
    }
};

restAdapter.setRequestInterceptor(requestInterceptor)
Erich
  • 2,743
  • 1
  • 24
  • 28
  • 1
    is [this](http://developer.android.com/reference/org/apache/http/HttpRequestInterceptor.html) what you're talking about? I cant seem to find reference to a class called RequestInterceptor. Or to RequestFacade. And RestAdapter doesn't have a method `setRequestInterceptor`. Can you provide some more details please? – Alfie Hanssen Aug 06 '13 at 16:01
  • You must have an older version of Retrofit. If you update it to version 1.1.1 you should see them. http://square.github.io/retrofit/javadoc/retrofit/RequestInterceptor.html – Erich Aug 06 '13 at 19:26
  • 1
    @kdubb: How do I do this for Body? I have a POST that accept a json payload. Thanks! – dannyroa Mar 21 '14 at 22:39
  • @dannyroa [RequestInterceptor](https://github.com/square/retrofit/blob/master/retrofit/src/main/java/retrofit/RequestInterceptor.java) allows you to set path params, query params or headers. I assume body was intentionally left out for two reasons: 1) Not all requests allow a body and 2) It would prevent you from setting any other Body – Erich Mar 21 '14 at 22:51
  • to help clarify, the `RequestInterceptor` is added via the `RestAdapterBuilder`, not the resulting `RestAdapter` (e.g. `myBuilder.setRequestInterceptor(myInterceptor);`) – rmirabelle Jun 08 '15 at 15:59
  • 2
    how to do this with retrofit 2.0.0.beta1? – M. Reza Nasirloo Sep 30 '15 at 18:26
  • @Pedram I added the answer for Retrofit 2 ;) – Louis CAD Oct 11 '15 at 12:16
  • @kdubb your website you link to tutorials for, seems to be infected with "JS:Injection-A [Trj]". I edited your answer to warn potential visitors. You may want to check your site (this https://stackoverflow.com/questions/34176949 may help) – Marcin Orlowski Feb 12 '16 at 17:14
  • I does not work with retrofit 2 here is link for retrofit 2:http://stackoverflow.com/questions/32948083/is-there-a-way-to-add-query-parameter-to-every-request-with-retrofit-2?noredirect=1&lq=1 – Kamil Nękanowicz Nov 03 '16 at 15:47