Requested URL not found

1.6k Views Asked by At

Code sample is Swift 3.0 running in Xcode 8(β3).

This Weather Underground URL (when MyKey is replaced with a valid key) returns a JSON string containing the current weather at the given location... http://api.wunderground.com/api/MyKey/conditions/q/51.3276,-1.0022.json

The code below should do the same thing but instead throws an error ("The requested URL was not found on this server.", details at bottom).

Can anyone see what I'm missing in the code?

private let key = "MyKey" // Substitute a valid Weather Underground key here.

@IBAction func fetchWeather() {
    let session = URLSession.shared
    let url     = URL(fileURLWithPath: "http://api.wunderground.com/api/\(key)/conditions/q/51.3276,-1.0022.json")

    let handler = { (data: Data?, response: URLResponse?, error: NSError?) -> Void in
        guard let data = data, let response = response, error == nil else {
            print("\nError: \(error?.description)\n")
            return
        }
        print("\nData: \(data)\n")
        print("\nResponse: \(response)\n")
    }

    let task    = session.dataTask(with: url, completionHandler: handler)
    task.resume()
}

Error: Optional("Error Domain=NSURLErrorDomain Code=-1100 \"The requested URL was not found on this server.\" UserInfo={NSUnderlyingError=0x600000442520 {Error Domain=kCFErrorDomainCFNetwork Code=-1100 \"(null)\"}, NSErrorFailingURLStringKey=file:///http:/api.wunderground.com/api/MyKey/forecast/q/51.3276,-1.0022.json, NSErrorFailingURLKey=file:///http:/api.wunderground.com/api/MyKey/forecast/q/51.3276,-1.0022.json, NSLocalizedDescription=The requested URL was not found on this server.}")

0

There are 0 best solutions below