8

I use Retrofit for most of my calls but in one of the cases, I have the full path provided in arguments. My URL is like this http://www.example.com/android.json. This URL is provided in full so I have to path it at runtime. I implement endpoint as suggested here https://medium.com/@kevintcoughlin/dynamic-endpoints-with-retrofit-a1f4229f4a8d but in the @GET I need to be able to put @GET(""). This does not work as I get an error saying I should provide at least one "/".

If I add the slash the URL becomes http://www.example.com/android.json/ and it does not work, the server returns forbidden. I also tried creating a custom GET interface similar to here https://github.com/square/retrofit/issues/458 but with GET and without providing a value method in the interface. Then I get another error saying value is missing.

Basically I need to be able to provide an empty or null value but retrofit does not allow that. How could I solve this problem? For now I am doing the JSON request manually but is there a way I could use retrofit for this case? I need to pass the full URL there is no way I can do endpoint http://www.example.com and @GET("/android.json"). Thanks

Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36
vallllll
  • 2,731
  • 6
  • 43
  • 77

3 Answers3

24

You can use @GET(".") to indicate that your url is the same as the base url.

@GET(".")
Observable<Result> getData(@Query("param") String parameter);
Dawit Abraham
  • 1,616
  • 15
  • 19
2

I've tried this approach, however didn't work for me.

Workaround for this issue is:

//Retrofit interface
public interface TestResourceClient {
    @GET
    Observable<Something> getSomething(@Url String anEmptyString);
}

//client call
Retrofit.Builder().baseUrl("absolute URL").build()
.create(TestResourceClient.class).getSomething("");

The downside of this solution is that you have to supply empty string in getSomething("") method call.

Tomas Bartalos
  • 1,256
  • 12
  • 29
0

I face the same problem with Retrofit 2. Using @GET, @GET("") and @GET(".") not solved my problem.

According to the official document you can the same baseUrl and @GET argument.

Endpoint values may be a full URL.

Values that have a host replace the host of baseUrl and values also with a scheme replace the scheme of baseUrl.
Base URL: http://example.com/
Endpoint: https://github.com/square/retrofit/
Result: https://github.com/square/retrofit/

So in my case:

interface MyAPI {
    @GET("http://www.omdbapi.com/")
    suspend fun getMovies(
        @Query("apikey") apikey: String,
        @Query("s") s: String
    ): Response<MoviesResponse>

    companion object {
        operator fun invoke(): MyAPI {
            return Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://www.omdbapi.com/")
                .build()
                .create(MyAPI::class.java)
        }
    }
}
Kaaveh Mohamedi
  • 1,399
  • 1
  • 15
  • 36