Hibbem
Hibbem

Reputation: 1547

Adding empty parameters to signature with Retrofit

I have an Android app that communicates with an API (Jersey with OAuth v1) that uses RQL. This is done by using the Retrofit 2 library.
To limit the data I've fetched, the limit(limit, offset) parameter is added to the URL. This wasn't easy, because Retrofit doesn't allow me to add empty parameters to a URL.
For example, this is my base URL: api.acme.com/v1 and this is the users endpoint: api.acme.com/v1/users. But when I want to limit the amount of returned users, I want to use api.acme.com/users?limit(10,2).

Because there was no option to add a variable name parameter with no value, the only method that didn't return an error was to generate the url like so:

@GET Call<UserList> getUsers (@Url String path);

and to pass this URL: http://api.acme.com/users?limit(10,2)

The error that returns is {"error":1,"error_name":"OAUTH_SIGNATURE_EXCEPTION","error_message":"The OAuth signature is invalid"}

This is because empty parameters are not added to the URL when generating the OAuth signature. Postman has the option to add these empty parameters to thesignature, but I can't seem to find this option in Retrofit. Any ideas ? Other options to add empty parameters to my URL are also always welcome ! I've already tried with Map, Paths and Query, but no results.

Upvotes: 1

Views: 998

Answers (1)

Emma
Emma

Reputation: 9343

If I understand your question properly, (assuming /v1/ is consistently there regardless of limit params, assuming you missed in 1 place in your question.), you can do this. This is a none standard way, though. It's a workaround and looks a bit ugly.

Retrofit baseUrl: "http://api.acme.com/v1/"

@GET("{user}")
Call<UserList> getUsers(@Path("user") String user);

If you do getUsers("users"), this resolves to "http://api.acme.com/v1/users"

If you do getUsers("users?limit(10,2)"), this resolves to "http://api.acme/com/v1/users?limit(10,2)". (You might add url encoding here.)

If you possibly can ask for a change in the API, I would recommend to change the limit to standard query parameters format.

ex) limit=X&offset=Y

In this format, you can simply use @Query.

Upvotes: 1

Related Questions