Skip to content

Instantly share code, notes, and snippets.

@arodbits
Forked from armstrongnate/basic-auth.swift
Last active August 29, 2015 14:17
Show Gist options
  • Save arodbits/9938fb8c23ff3e48777f to your computer and use it in GitHub Desktop.
Save arodbits/9938fb8c23ff3e48777f to your computer and use it in GitHub Desktop.
import Foundation
let config = NSURLSessionConfiguration.defaultSessionConfiguration()
let userPasswordString = "[email protected]:password"
let userPasswordData = userPasswordString.dataUsingEncoding(NSUTF8StringEncoding)
let base64EncodedCredential = userPasswordData!.base64EncodedStringWithOptions(nil)
let authString = "Basic \(base64EncodedCredential)"
config.HTTPAdditionalHeaders = ["Authorization" : authString]
let session = NSURLSession(configuration: config)
var running = false
let url = NSURL(string: "https://example.com/api/v1/records.json")
let task = session.dataTaskWithURL(url) {
(let data, let response, let error) in
if let httpResponse = response as? NSHTTPURLResponse {
let dataString = NSString(data: data, encoding: NSUTF8StringEncoding)
println(dataString)
}
running = false
}
running = true
task.resume()
while running {
println("waiting...")
sleep(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment