Home > Blockchain >  How to pass http method type dynamically to retrofit
How to pass http method type dynamically to retrofit

Time:11-25

I am making a method which can be used dynamically for all type of requests say, POST,GET and all others.

I have implemented a method like this

 @HTTP(method = "{method}", path = "{url}")
    fun callAPIs(
        @Path(value = "method") httpMethod: String,
        @Header("Authorization") auth: String,
        @Header("app-version") app_version: String,
        @Body data: JsonObject,
        @Url url: String
    ): Call<JsonElement>

But I am getting error. Can anyone help me solve this error?

 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.myapplication, PID: 17793
    java.lang.IllegalArgumentException: URL "{url}" does not contain "{method}". (parameter #1)
        for method APICallInterface.callAPIs

CodePudding user response:

This is answered in this answer. https://stackoverflow.com/a/37546082/4491971

Retrofit is not designed for dynamic url requests. You configure retrofit with your API base URL then make specific requests. For a more flexible dynamic option use out OkHttp. It is the HTTP Client used by Retrofit and it easy to work with.

  • Related