Error Domain=NSURLErrorDomain Code=-1005 对 Uber 的 curl 请求-api

Error Domain=NSURLErrorDomain Code=-1005 curl request to Uber-api

我正在使用 swift 编写应用程序并尝试集成 Uber api。 我想在终端中执行这个完全简单(!?)的请求:

curl -X GET -G 'https://sandbox-api.uber.com/v1/products' -d server_token=*************************************** -d latitude=21.3088619 -d longitude=-157.8086674

为此,我尝试了以下代码:

    func performPostUberRequest() {
        let url: NSURL = NSURL(string: "https://sandbox-api.uber.com/v1/products")
        print("performPostUberRequest Ignited")
        let params:[String: AnyObject] = ["server_token" : "***************************************", "latitude" : "21.3088621", "longitude" : "-157.8086632"]
        let request: NSMutableURLRequest = NSMutableURLRequest(URL: url)
        request.HTTPMethod = "GET"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        do {
            try request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
        } catch {

        }
        print("performPostUberRequest ongoing")
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){

            response, data, error in
            if let error = error {
                print("error: ")
                print(error)
                print("data: ")
                print(data)
                print("response: ")
                print(response)
            } else if data != nil {
                do {
                    let json: NSDictionary = try NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions()) as! NSDictionary
                    print("here comes JASON: ")
                    print(json)

                } catch {
                    print("Epic Fail")
                }
            }
        }
    }

但无论我尝试了 3-4 天,现在都给出了完全相同的答案:

performPostUberRequest Ignited
performPostUberRequest ongoing
error: 
Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/products, _kCFStreamErrorCodeKey=-4, NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/products, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4, NSUnderlyingError=0x16da79f0 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, NSErrorFailingURLStringKey=https://sandbox-api.uber.com/v1/products, NSErrorFailingURLKey=https://sandbox-api.uber.com/v1/products, NSLocalizedDescription=The network connection was lost., _kCFStreamErrorDomainKey=4}}}
data: 
nil
response: 
nil

编辑:

我下载了 Charles,它给了我以下屏幕:

令我困扰的是 "SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations" 这句话,另外,当我通过终端输入 curl 命令时,我无法在 Charles 上触发任何东西。我想这很正常。

SO 上的每个解决方案都告诉我重新启动模拟器,我照做了。我去了模拟器设置并启用了 HTTP 服务。我吹了墨盒,然后转到 Google 的第 2 页。请。帮助。我.

编辑 2:解决方案

这是最终代码,感谢@faarwa

    func performPostUberRequest() {
        var components = NSURLComponents()
        components.scheme = "https"
        components.host = "sandbox-api.uber.com"
        components.path = "/v1/products"
        components.queryItems = [
            NSURLQueryItem(name: "server_token", value: "********"),
            NSURLQueryItem(name: "latitude", value: "21.3088621"),
            NSURLQueryItem(name: "longitude", value: "-157.8086632")
        ]
        let request: NSMutableURLRequest = NSMutableURLRequest(URL: components.URL!)
        NSURLConnection.sendAsynchronousRequest(request, queue: NSOperationQueue.mainQueue()){
print("thanks faarwa")
 }

通过 URL 发送查询参数,而不是 HTTP 消息正文,因为它是 GET 请求。删除:

    do {
        try request.HTTPBody = NSJSONSerialization.dataWithJSONObject(params, options: NSJSONWritingOptions())
    } catch {

    }

并将参数添加到 NSURL(例如使用 NSURLComponents)应该可以解决这个问题。