Ghulam Rasool
Ghulam Rasool

Reputation: 78

Retrofit GET Query with api key:

Hello I am trying to use Retrofit to access a URL : https://my.api.mockaroo.com/products?key=57b501f0

here is my interface:

interface ProductInterface {
    @GET("products?")
    suspend fun getTodos(@Query("key") key: String): Response<List<product>>
}

here is my Retrofit Object

object RetrofitInstance {
    val api: ProductInterface by lazy<ProductInterface> {
        Log.e("Request", "Sent")
        Retrofit.Builder()
            .baseUrl("https://my.api.mockaroo.com/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(ProductInterface::class.java)
    }
}

and here it is how i am accessing it:

RetrofitInstance.api.getTodos("57b501f0")

But When i logged its working, It says following URL: https://my.api.mockaroo.com/products?&key=57b501f0

was accessed instead of : https://my.api.mockaroo.com/products?key=57b501f0

How can i resolve this?

Upvotes: 1

Views: 2532

Answers (3)

Mike Schvedov
Mike Schvedov

Reputation: 156

Most of your code is fine, except the "?" sign.

  @GET("products?")

It is unnecessary. When you create a @Query retrofit automatically creates a question mark for you.

Anyway, passing an API key using a @Query and even inside the @GET is not recommended, because if you have multiply requests they will probably require the same API key. So passing it again and again is just ugly duplicated code.

Instead you should use Interceptors, if you need to send the same parameter to many requests.

For example:

 public void getDataUsingRetrofit() {

    // Add the interceptor to OkHttpClient
    OkHttpClient.Builder builder = new OkHttpClient.Builder();
    builder.addInterceptor(new Interceptor() {
        @NotNull
        @Override
        public Response intercept(@NotNull Chain chain) throws IOException {
            var request = chain.request().newBuilder();
            var originalHttpUrl = chain.request().url();
            var url = originalHttpUrl.newBuilder()
                  .addQueryParameter("key", "***your api key ***").build();
            request.url(url);
            return chain.proceed(request.build());
        }
    });

    // Creating a client out of the builder
    OkHttpClient client = builder.build();

    // Retrofit
    Retrofit retrofit = new Retrofit.Builder()
            // We only need the base url
            .baseUrl("*** your base url ***")
            .addConverterFactory(GsonConverterFactory.create())
            // Set the custom client when building adapter
            .client(client)
            .build();               ... rest of method ... }

With this you don't need to send the API key anymore

interface ProductInterface {
@GET("products")
suspend fun getTodos(): Response<List<product>>

}

Upvotes: 2

Rohini
Rohini

Reputation: 21

In retrofit when you pass the queries,it will append to the URL with questionmark(?).if there is more than one queries is append then it uses the & sign for that so you have already mentioned the ? in the @GET("products?") so its considering the query added in the request as extra so its using the & for that.please remove the ? from the @GET("products?") .it will work fine

interface ProductInterface {
    @GET("products")
    suspend fun getTodos(@Query("key") key: String): Response<List<product>>
}

Refer this link for more info about Api Request

Upvotes: 0

Rudrank Basant
Rudrank Basant

Reputation: 91

Try this way maybe:

interface ProductInterface {
    @GET("products?key=57b501f0")
    suspend fun getTodos(): Response<List<product>>
}

AND instead of RetrofitInstance.api.getTodos("57b501f0") write

RetrofitInstance.api.getTodos()

-----OR-----

Just remove the question mark after products.

interface ProductInterface {
    @GET("products")
    suspend fun getTodos(@Query("key") key: String): Response<List<product>>
}

Upvotes: 0

Related Questions