Search code examples
androidretrofit2

How can i use a complete endpoint URL to make a retrofit call, instead of using base/relative URLs?


I have to make a post request using retrofit, but the URL for this request comes from another request (GET), and the URL comes as a complete endpoint (i.e: https://pod-000-1005-03.backblaze.com/b2api/v2/b2_upload_file?cvt=c001_v0001005_t0027&bucket=4a48fe8875c6214145260818).

How can i make a retrofit request directly to this endpoint?

How im creating the retrofit instance:

fun getUploadApi(uploadUrl: String): B2UploadApi {
        return Retrofit.Builder()
            .baseUrl(uploadUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build()
            .create(B2UploadApi::class.java)
    }
  • this throws an exception since the url doesnt end with '/'

And the POST request:

@POST
    suspend fun uploadFile(
        @Header("Authorization") authToken: String,
        @Header("X-Bz-File-Name") fileName: String,
        @Header("Content-Length") fileSize: Int,
        @Header("Content-Type") mimeType: String,
        @Header("X-Bz-Content-Sha1") sha1: String,
        @Body byteArray: ByteArray
    ): Response<UploadResponse>

Solution

  • As mentioned in documentation , @url will override base url which you have mentioned at time of retrofit object creation

    So you just need to use @url annotation along with method in retrofit service

    documentation - https://square.github.io/retrofit/2.x/retrofit/retrofit2/http/Url.html

    example -

    https://futurestud.io/tutorials/retrofit-2-how-to-use-dynamic-urls-for-requests