Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.89 KB | None | 0 0
  1. var request = URLRequest(url: URL(string: "https://api.thingspeak.com/update")!)
  2. request.httpMethod = "POST"
  3. let postString = "api_key=H3MQFPAJ1IOK1I0W&field1=2"
  4. request.httpBody = postString.data(using: .utf8)
  5. let task = URLSession.shared.dataTask(with: request) { data, response, error in
  6. guard let data = data, error == nil else {
  7. print("error=\(error)")
  8. return
  9. }
  10.  
  11. if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
  12. print("statusCode should be 200, but is \(httpStatus.statusCode)")
  13. print("response = \(response)")
  14. }
  15.  
  16. let responseString = String(data: data, encoding: .utf8)
  17. print("responseString = \(responseString)")
  18. }
  19. task.resume()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement