Advertisement
Guest User

Untitled

a guest
Jul 16th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. func doLogin(){
  2. print("entered doLogin")
  3. let myUrl: String = "https://url.com/names?login"
  4. guard let url = URL(string: myUrl.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!)else {
  5. print("Error: cannot create URL")
  6. return
  7. }
  8. var request = URLRequest.init(url: url)
  9. request.httpMethod = "POST"
  10.  
  11. let params = ["Username":String(describing: self.username), "Password":String(describing: self.password)]
  12. request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
  13. request.addValue("application/json", forHTTPHeaderField: "Content-Type")
  14.  
  15. //URLSession.shared.dataTask(with: request) { (data:Data?, response:URLResponse?, error:Error?) in
  16. // if let safeData = data{
  17. // print("response: (String(data:safeData, encoding:.utf8))")
  18. // }
  19. //}
  20.  
  21. //let postString = "Username=(String(describing: self.username))&Password=(String(describing: self.password))"
  22. //request.httpBody = postString.data(using:String.Encoding.utf8)
  23. request.timeoutInterval = 30.0
  24.  
  25. //print(request)
  26.  
  27. let configuration = URLSessionConfiguration.default
  28. let session = URLSession(configuration : configuration)
  29. //let session = URLSession(configuration: configuration, delegate: self as! URLSessionDelegate, delegateQueue: nil)
  30.  
  31. let task = session.dataTask(with: request) {
  32. (data : Data?, response:URLResponse?, error) in
  33. // check for any errors
  34. guard error == nil else {
  35. print("error calling POST on url")
  36. print(error!)
  37. return
  38. }
  39. // make sure we got data
  40. guard let responseData = data else {
  41. print("Error: did not receive data")
  42.  
  43. return
  44. }
  45.  
  46. do {
  47. print ("responseData:")
  48. print(responseData)
  49. }
  50. }
  51. //print ("response:")
  52. //print(task.response)
  53. task.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement