Retrofit URL resolution

How the url is built from base_url + endpoint

For a more complete and in-depth explanation, check out our complete Retrofit course

Download the starter project here

Github repository

URL resolution simply means how Retrofit puts together your BASE_RUL and your endpoint. Normally, it simply takes the two and concatenates them together. But there is a bit more detail to that.

BASE_URL should end in a /

If you forget to add a forward slash, Retrofit will automatically add it for you.

The endpoint should not begin with a /

private val BASE_URL = “https://example.com/user/info”

private val ENDPOINT = “getInfo”

Retrofit will concatenate those two together to obtain the final URL

URL = “https://example.com/user/info/getInfo”

What happens if we put a forward slash at the beginning of the endpoint? In that case the URL that is built will only have the path getInfo, as it’s considered the root, and will remove the rest of the path.

private val BASE_URL = “https://example.com/user/info”

private val ENDPOINT = “getInfo”

URL = “https://example.com/getInfo”

Only use the forward slash at the beginning only if you require this functionality, otherwise it’s common practice to not put the beginning slash and let Retrofit construct the URL through concatenation.

For a more complete and in-depth explanation, check out our complete Retrofit course

Close Bitnami banner
Bitnami