Skip to content

Instantly share code, notes, and snippets.

@stremsdoerfer
Last active December 22, 2017 10:18
Show Gist options
  • Save stremsdoerfer/6ccd9ad079526fa8630bc0106fa42896 to your computer and use it in GitHub Desktop.
Save stremsdoerfer/6ccd9ad079526fa8630bc0106fa42896 to your computer and use it in GitHub Desktop.
POST request example in Swift
var request = URLRequest(url:URL(string:"http://localhost:8888/login")!)
request.httpMethod = "POST"
let params = ["email":"[email protected]", "password":"password"]
request.httpBody = try? JSONSerialization.data(withJSONObject: params, options: [])
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
URLSession.shared.dataTask(with: request) { (data:Data?, response:URLResponse?, error:Error?) in
if let safeData = data{
print("response: \(String(data:safeData, encoding:.utf8))")
}
}
@sivakg2000
Copy link

sivakg2000 commented Oct 13, 2017

It dosen't work. Have to add

resume

let task=URLSession.shared.dataTask(with: request) { (data:Data?, response:URLResponse?, error:Error?) in
  if let safeData = data{
    print("response: \(String(data:safeData, encoding:.utf8))")
  }
}
task.resume()

@lu15fer
Copy link

lu15fer commented Nov 24, 2017

huevos puto

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment