Send HTTP Request in Swift
If you want to make send the HTTP Request in Swift.You have your URL and some parameters to the URL you want to send the Request.Firstly, you have to convert the BASEDATA into the DATA in utf8 then you make the UrlRequest and the UrlSession and create the task of your Session. here is the code how to Send HTTP Request in Swift : –
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
let encodedData = BASEDATA.data(using: String.Encoding.utf8)! let stringUrl = BASEURL + "homepage/" + "?width=" + String(format: "%f" , SCREEN_WIDTH) let url:URL = URL(string: stringUrl)! let session = URLSession.shared var request = URLRequest(url: url) request.httpMethod = "GET" request.cachePolicy = NSURLRequest.CachePolicy.reloadIgnoringCacheData let base64String = encodedData.base64EncodedString() request.addValue("application/json", forHTTPHeaderField: "Content-Type") request.addValue("application/json", forHTTPHeaderField: "Accept") request.setValue("Basic \(base64String)", forHTTPHeaderField: "Authorization") DispatchQueue.main.async(execute: {() -> Void in let task = session.dataTask(with: request as URLRequest) { ( data, response, error) in guard let data = data, let _:URLResponse = response , error == nil else { print("error") return } self.dataString = String(data: data, encoding: String.Encoding.utf8)! if(self.dataString.characters.count > 0){ self.doFurtherProcessing() } } task.resume() }) |