iosswiftnsurlcomponents

Avoid percent encoding of host in URLComponents


Using URLComponents, is there a way to avoid percent encoding of host?

var components = URLComponents()
components.scheme = "https"
components.host = "endpoint.com/v1/api.php?"
// ends up as https://endpoint.com%2Fv1%2Fapi.php%3F? but I want it to stay as https://endpoint.com/v1/api.php?

This question is about avoiding encoding, not adding encoding as the question linked as duplicate states.


Solution

  • "/v1/api.php" is not part of the host, but rather the path.

    Use this:

    var components = URLComponents()
    components.scheme = "https"
    components.host = "endpoint.com"
    components.path = "/v1/api.php"