GET request with parameters

前端 未结 2 2112
萌比男神i
萌比男神i 2020-12-19 23:05

Which way is recomed in swift 3 for GET with parameters ?

Example :

https://my-side.com/data?token=AS7F87SAD84889AD/

Thanks in ad

2条回答
  •  自闭症患者
    2020-12-19 23:15

    Well I'm handling my http requests like this:

    func getData(completionHandler: @escaping ((result:Bool)) -> ()) {
    
            // Asynchronous Http call to your api url, using NSURLSession:
            guard let url = URL(string: "https://my-side.com/data?token=AS7F87SAD84889AD/") else {
                print("Url conversion issue.")
                return
            }
    
            var request = URLRequest(url: url)
    
            request.httpMethod = "GET"
    
            URLSession.shared.dataTask(with: request, completionHandler: { (data, response, error) -> Void in
                // Check if data was received successfully
                if error == nil && data != nil {
                    do {
                        // Convert NSData to Dictionary where keys are of type String, and values are of any type
                        let json = try JSONSerialization.jsonObject(with: data!, options: JSONSerialization.ReadingOptions.mutableContainers) as! [String:AnyObject]
    
                        //do your stuff
    
                        completionHandler(true)
    
                    } catch {
                        completionHandler(false)
                    }
                }
                else if error != nil
                {
                    completionHandler(false)
                }
            }).resume()
        }
    

提交回复
热议问题