John Sorensen
John Sorensen

Reputation: 960

Give my URLs bodies using the URLComponents approach in Swift

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!
}

Upvotes: 1

Views: 486

Answers (1)

Rob Napier
Rob Napier

Reputation: 299345

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.

Upvotes: 2

Related Questions