Last active
December 22, 2017 10:18
-
-
Save stremsdoerfer/6ccd9ad079526fa8630bc0106fa42896 to your computer and use it in GitHub Desktop.
POST request example in Swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
It dosen't work. Have to add