Search code examples
androidkotlinretrofitokhttp

Additional Query depending on boolean


I have query like that:

getData(number: String)

@GET("/myapp/{number}/details")
fun getData(
@Path number: String
)

But I would like to create something like:

getData(withAdditionalParam: Boolean, number: String)


@GET("/myapp/{number}/details?sometimesAdditionalParam="number")
    fun getData(
    @Path number: String,
    ? what here ?
    )

At the moment I have 2 separate GET functions but I belive we can achieve it using 1 get function


Solution

  • You can make the additional param nullable so that Retrofit only passes it if it's not null.

    @GET("/myapp/{number}/details")
        fun getData(
        @Path number: String,
        @Query("sometimesAdditionalParam") additionalParam : Boolean? = null
        )
    

    when you want to pass it

    getData(number: String, true)
    

    when you don't

    getData(number: String)