在 Swift 中使用 URLComponents 方法提供我的 URL 正文

Give my URLs bodies using the URLComponents approach in Swift

我正在构建我的 API 架构,并想开始以编程方式编写我的 URL。但是,我不清楚如何使用 Swift 中的 URLComponents 方法提供我的 URL 主体。是用components.query方法吗?

到目前为止的代码:

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

URL 不包括 body。 headers 和身体都不同于 URL。如果您的 Web 服务支持在查询中接收参数,那么这可能适合您,但它与 body 不同。 URLComponents 只是构建和解析 URL.

的有用工具

如果您想合并 URL、headers、body 和一些政策,请参阅 URLRequest