Example how to use URLQueryItem
for the requests.
func getRequest(params: [String:String]) {
let urlComp = NSURLComponents(string: "https://my-side.com/data")!
var items = [URLQueryItem]()
for (key,value) in params {
items.append(URLQueryItem(name: key, value: value))
}
items = items.filter{!$0.name.isEmpty}
if !items.isEmpty {
urlComp.queryItems = items
}
var urlRequest = URLRequest(url: urlComp.url!)
urlRequest.httpMethod = "GET"
let config = URLSessionConfiguration.default
let session = URLSession(configuration: config)
let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
})
task.resume()
}
getRequest(params: ["token": "AS7F87SAD84889AD"])
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…