Skip to content

Instantly share code, notes, and snippets.

@rvlb
Created May 5, 2017 20:06
Show Gist options
  • Save rvlb/7b5f6431198b31c537d71d755e08fee7 to your computer and use it in GitHub Desktop.
Save rvlb/7b5f6431198b31c537d71d755e08fee7 to your computer and use it in GitHub Desktop.
Módulos CITi
// Create an array of URLQueryItem that represent the key-value pairs of
// the query.
var queryItems = [URLQueryItem]()
queryItems.append(URLQueryItem(name: "key", value: "value"))
// Build the query with the data previously set
var query = NSURLComponents()
query.queryItems = queryItems
// Create an URLRequest using a previously defined url and set its method
// as POST
var request = URLRequest(url: url)
request.method = "POST"
// Set the request body as the data set in query
request.httpBody = query.query!.data(using: .utf8)
// Send request (you can also put this inside a var if you wish)
URLSession.shared.dataTask(with: request) { (data, response, error) in
// Do stuff the same way as in GET
}.resume()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment