Search code examples
httpsalamofirexcode9swift4macos-high-sierra

URLSession or Alamofire.Request don't work with dashes in URL?


I'm trying to make a request to the following URL

https://cactus.nci.nih.gov/chemical/structure/530-62-1/smiles

this request should just return a plain text:

Clc1ccc(C=O)cc1

Using the code in a playground like this works perfectly fine:

var cas = "530-62-1"
let url = URL(string: "https://cactus.nci.nih.gov/chemical/structure/\(cas)/smiles")
var result = ""

let task = URLSession.shared.dataTask(with: url!) {(data, response, error) in

    //print(response!)
    result = String(data: data!, encoding: String.Encoding(rawValue: String.Encoding.utf8.rawValue))!
    print(result)
}

task.resume()

But if I try to use the code in my macOS project (using Swift 4 and Xcode 9) targeting macOS 10.13 I get

URL for request is: https://cactus.nci.nih.gov/chemical/structure/104-88-1/smiles 2017-10-10 17:23:08.475739+0100 AimieSmiles[18012:3495056] dnssd_clientstub ConnectToServer: connect() failed path:/var/run/mDNSResponder Socket:11 Err:-1 Errno:1 Operation not permitted 2017-10-10 17:23:08.476341+0100 AimieSmiles[18012:3495056] [] nw_resolver_create_dns_service_locked DNSServiceCreateDelegateConnection failed: ServiceNotRunning(-65563) 2017-10-10 17:23:08.476719+0100 AimieSmiles[18012:3495056] TIC TCP Conn Failed [1:0x60c000162dc0]: 10:-72000 Err(-65563)

If I try using Alamofire (I just wanted to check that as a possibility, using a code like this one:

func alamoRequest(cas: String) -> String {

        var smilesResult = ""
        var stringURL = "https://cactus.nci.nih.gov/chemical/structure/\(cas)/smiles"
        var url = URL(string: stringURL)!
        Alamofire.request(url).responseString { response in
            print("Request: \(String(describing: response.request))")   // original url request
            print("Response: \(String(describing: response.response))") // http url response
            print("Result: \(response.result)")                         // response serialization result
            print("Error: \(String(describing: response.error))")



            if let result = response.result.value {
                smilesResult = result
                print("Result: \(result)") // serialized json response
            } else if let data = response.data, let utf8Text = String(data: data, encoding: .utf8) {
                smilesResult = utf8Text
                print("Data: \(utf8Text)") // original server data as UTF8 string
            }
        }

        return smilesResult
    }

I get the following error:

Request: Optional(https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles) Response: nil Result: FAILURE Error: Optional(Error Domain=NSURLErrorDomain Code=-1003 "A server with the specified hostname could not be found." UserInfo={NSUnderlyingError=0x60c000450800 {Error Domain=kCFErrorDomainCFNetwork Code=-1003 "(null)" UserInfo={_kCFStreamErrorCodeKey=-72000, _kCFStreamErrorDomainKey=10}}, NSErrorFailingURLStringKey=https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles, NSErrorFailingURLKey=https://cactus.nci.nih.gov/chemical/structure/514-10-3/smiles, _kCFStreamErrorDomainKey=10, _kCFStreamErrorCodeKey=-72000, NSLocalizedDescription=A server with the specified hostname could not be found.})

It looks from the errors that the URL is not valid, or not recognised from a macOS App? Strangely it works fine from a Playground...

I tried enabling App Transport Security Settings --> Allow Arbitrary Loads --> YES in the plist but that didn't make a difference.


Solution

  • The problem turned out to be trivial: I just needed to enable App Sandbox and check incoming and outgoing network connections.