I'm attempting to write a simple API client in Swift 2.2 using NSURLSession
for a server I'm running locally at port 3000. The server just serves up a static string of JSON.
This works fine:
$ curl http://localhost:3000
{"data":"value"}
My Swift code to hit the API is:
// api_client.swift
let url = NSURL(string: "http://localhost:3000/api")
let task = NSURLSession.sharedSession().dataTaskWithURL(url!) {(data, response, error) in
print("The response was:")
print(NSString(data: data!, encoding: NSUTF8StringEncoding))
}
print("Running the request...")
task.resume()
I'm attempting to run it from the command line
$ swift api_client.swift
Running the request...
But that's all that happens. I don't see the "The response was" line printed, or the response from the server. When I check the server logs it shows no request came in at all.
What am I doing wrong? I'm very new to Swift and am having trouble figuring this out. I'm on Swift 2.2 and XCode 7.3