Created
December 24, 2015 05:48
-
-
Save paulomcnally/5e20b5b0e0223fad5e7f to your computer and use it in GitHub Desktop.
HTTP Request in Swift 2.0 ref: http://qiita.com/Shirade/items/80d2ed2114d7352a7551
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
do { | |
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions.init(rawValue: 2)) | |
} catch { | |
// Error Handling | |
print("NSJSONSerialization Error") | |
return | |
} |
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
import Foundation | |
class Request { | |
let session: NSURLSession = NSURLSession.sharedSession() | |
// GET METHOD | |
func get(url: NSURL, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) { | |
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url) | |
request.HTTPMethod = "GET" | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
session.dataTaskWithRequest(request, completionHandler: completionHandler).resume() | |
} | |
// PUT METHOD | |
func post(url: NSURL, body: NSMutableDictionary, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) { | |
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url) | |
request.HTTPMethod = "POST" | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
do { | |
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions.init(rawValue: 2)) | |
} catch { | |
// Error Handling | |
print("NSJSONSerialization Error") | |
return | |
} | |
session.dataTaskWithRequest(request, completionHandler: completionHandler).resume() | |
} | |
// POST METHOD | |
func put(url: NSURL, body: NSMutableDictionary, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) { | |
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url) | |
request.HTTPMethod = "PUT" | |
request.addValue("application/json", forHTTPHeaderField: "Content-Type") | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
do { | |
request.HTTPBody = try NSJSONSerialization.dataWithJSONObject(body, options: NSJSONWritingOptions.init(rawValue: 2)) | |
} catch { | |
// Error Handling | |
print("NSJSONSerialization Error") | |
return | |
} | |
session.dataTaskWithRequest(request, completionHandler: completionHandler).resume() | |
} | |
// DELETE METHOD | |
func delete(url: NSURL, completionHandler: (NSData?, NSURLResponse?, NSError?) -> Void) { | |
let request: NSMutableURLRequest = NSMutableURLRequest(URL: url) | |
request.HTTPMethod = "DELETE" | |
request.addValue("application/json", forHTTPHeaderField: "Accept") | |
session.dataTaskWithRequest(request, completionHandler: completionHandler).resume() | |
} | |
} |
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
let request: Request = Request() | |
let url: NSURL = NSURL(string: "https://api.example.com/path/to/resource")! | |
let body: NSMutableDictionary = NSMutableDictionary() | |
body.setValue("value", forKey: "key") | |
request.post(url, body: body, completionHandler: { data, response, error in | |
// code | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment