Home > other >  Give my URLs bodies using the URLComponents approach in Swift
Give my URLs bodies using the URLComponents approach in Swift

Time:11-03

I'm building my API architecture and want to start programmatically composing my URLs. However, I am not clear on how to give my URLs bodies using the URLComponents approach in Swift. Is it with the components.query method?

Code so far:

func getURL(scheme: String, host: String, port: Int, path: String) -> URL {
    var components = URLComponents()
    components.scheme = scheme
    components.host = host
    components.port = port
    components.path = path
    return components.url!
}

CodePudding user response:

An URL does not include a body. Both headers and bodies are distinct from an URL. If your web service supports receiving parameters in the query, then that may work for you, but it it's distinct from the body. URLComponents is just a useful tool for building and parsing an URL.

If you want to combine an URL, headers, body, and some policies, see URLRequest.

  • Related