Swift: preventing the host url component from being percent escaped automatically

661 Views Asked by At

I have the following two base urls for my API methods:

let stagingHost = "myApi.test.com/sub1"

let productionHost = "myApi.test.com/sub2"

when I build the url for any API call in my app, I do the following:

var components = URLComponents()
components.scheme = "https"
components.host = stagingHost //or productionHost doesn't matter
components.path = "apiMethodPath"
let url = components.url

this will result in the following url:

https://myApi.test.com%2Fsub1/apiMethodPath which is an invalid URL for my api due to the automatically added %2F characters to the host component, how to prevent the host component from precent escaping its url string ?

p.s. I can't add /sub1 and /sub2 as a path component to a lot of api methods depending on the chosen environment (staging or production), because it's going to be a time consuming process.

0

There are 0 best solutions below