Swift - contentsOfURL returns 'nil' with specific site, but it works on browser

312 views Asked by At

This code works with other URL's, but not with this one. It always returns nil with "contentsOfURL". Why?

var url = NSURL(string: "http://www.bsnpr.com/api/standing.asp?serie=1&liga=1&y=2015")
var err: NSError?
var data = NSData(contentsOfURL: url!, options: nil, error: &err)

"err" delivers the message:

"Optional(Error Domain=NSCocoaErrorDomain Code=256 "The operation couldn’t be completed. (Cocoa error 256.)" UserInfo=0x7f8f32c2e1e0 {NSURL=http://www.bsnpr.com/api/standing.asp?serie=1&liga=1&y=2015})"

Any ideas? Thanks in advance.

1

There are 1 answers

0
Eric Aya On

Your server delivers data with Transfer-Encoding set to chunked, and NSData doesn't like it.

You can get your data with a REST framework like Alamofire:

Alamofire.request(.GET, "http://www.bsnpr.com/api/standing.asp?serie=1&liga=1&y=2015")
    .responseJSON { (request, response, json, error) in
        println(json) // works with your url, I just tested
}