Search code examples
androidurlokhttptrailing-slash

Forcing trailing slash before query params with OkHttp3


I have a String url with queries (https://example.com/path/segments?q1=1&q2=2 for example). I need to add trailing slash (https://example.com/path/segments/?q1=1&q2=2) before queries if it does not exist using okhttp 3. How can I do this?


Solution

  • Use httpUrlBuilder.addPathSegment("") to force a trailing slash at the end of the path. HttpUrl models each path segment as being prefixed by a /, with an empty string indicating a lone slash.