Skip to content

Instantly share code, notes, and snippets.

@29satnam
Created July 16, 2016 10:58
Show Gist options
  • Save 29satnam/184818411744a0ef87fc480d0075a17d to your computer and use it in GitHub Desktop.
Save 29satnam/184818411744a0ef87fc480d0075a17d to your computer and use it in GitHub Desktop.
Swift HTTP GET Example
let session = NSURLSession.sharedSession()
let url = NSURL(string: "http://someurl.com")
let request = NSMutableURLRequest(URL: url!)
request.HTTPMethod = "GET"
let task = session.dataTaskWithRequest(request, completionHandler: {data, response, error -> Void in
if (data != nil) {
let res = response as! NSHTTPURLResponse!;
if 200..<300 ~= res.statusCode {
do {
let jsonData:NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options:NSJSONReadingOptions.MutableContainers ) as! NSDictionary
// parsing resonse
let username:String = jsonData.valueForKey("username") as! String
print(username)
} catch _ as NSError {
}
}
}
})
task.resume()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment