Created
November 5, 2016 01:35
-
-
Save dmathewwws/63064a4114fba3769448602208f66c0d to your computer and use it in GitHub Desktop.
URLSession POST request example
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
private static func createUserEventData(user:SKUser, eventType:SKEventType, sticker:Sticker?) { | |
// server endpoint | |
let endpoint = "https://app.stickerkit.io/userEvent/v1/\(user.projectID)" | |
guard let endpointUrl = URL(string: endpoint) else { | |
return nil | |
} | |
//Make JSON to send to send to server | |
var json = [String:Any]() | |
json[SKUser.PropertyKey.UUID] = user.UUID | |
json[SKUser.PropertyKey.projectID] = user.projectID | |
json[SKUser.PropertyKey.countryCode] = user.countryCode | |
json[SKUser.PropertyKey.deviceType] = user.deviceType | |
json[SKEvent.PropertyKey.type] = eventType.rawValue | |
json[SKEvent.PropertyKey.stickerID] = sticker?.id ?? "" | |
do { | |
let data = try JSONSerialization.data(withJSONObject: json, options: []) | |
var request = URLRequest(url: endpointUrl) | |
request.httpMethod = "POST" | |
request.httpBody = data | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
let task = URLSession.shared.dataTask(with: request) | |
task.resume() | |
}catch{ | |
} | |
} |
Hello,
I need to get data result return from POST API. How to get it ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks, could not get it working without https://gist.github.com/dmathewwws/63064a4114fba3769448602208f66c0d#file-urlsession-post-swift-L26-L27