eduardo eduardo - 27 days ago 11
HTTP Question

Http request command line tool

I am developing a Command Line Tool in Swift 3, i have this code:

let url = "www.google.com"
var request = URLRequest(url:url)
request.httpMethod = "GET"

let task = session.dataTask(with: a){ (data,response,error) in

print("REACHED")
handler(response,data)


}
task.resume()


I cannot reach the task if i use "http://" or "https://" as prefix in any url, i am wondering if i need a App Transport Security plist, i already tried create a simple plist, anyone knows some if has a particularity for this problem?

Rob Rob
Answer
  1. When specifying a URL, you need the "scheme" (e.g. http:// or https://).

  2. The url is a URL, not a String, so it should be:

    let url = URL(string: "http://www.google.com")!
    
  3. Yes, you need Info.plist entry if you want to use http://. E.g. http://stackoverflow.com/a/37552442/1271826 or Transport security has blocked a cleartext HTTP or just search stack overflow for "[ios] http info.plist" or with [osx].

    Note, in Xcode 8.1, console apps don't necessarily have a Info.plist file, so if you don't have one, you may have to add one by pressing command+n:

    add plist

    update your target settings to specify the plist:

    plist target settings

    and then add the appropriate settings, e.g.:

    enter image description here

  4. I assume where you have URLRequest(with: a) you meant URLRequest(with: request).

  5. You'll need something to keep the command app alive while you're performing the request (e.g. a semaphore or something like that).