Home > front end >  How do I always pass a constant argument in a Retrofit call?
How do I always pass a constant argument in a Retrofit call?

Time:10-06

How can I always pass a query parameter with a hard-coded value in a Retrofit2 call, without exposing that parameter to users of my API?

I am calling an API I don't own, and it can take an obscure argument. I always want to pass "magic" for that argument, and because it's an obscure implementation detail, I don't want my callers have to deal with it.

I have code like this:

interface MyApiClient {
    data class MyResponse(val result: String?)

    @GET("/some/api/call")
    fun someApiCall(
        @Query("mandatoryArg") arg: String,
        @Query("obscureArg") obscureArg: String = "magic"
    ): Call<MyResponse>
}

The best I could come up with was making obscureArg optional and giving it a good default, but this way callers can still pass something else.

How do I always pass obscureArg=magic in my REST API call without allowing callers to pass something else?

CodePudding user response:

You can hardcode the query parameter in the url itself.

@GET("/some/api/call?obscureArg=magic")
fun someApiCall(
    @Query("mandatoryArg") arg: String,
): Call<MyResponse>
  • Related