Quentin Malgaud
Quentin Malgaud

Reputation: 415

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

I am writing an app using swift and trying to integrate Uber api. I would like to do this exact simple(!?) request I do in the terminal:

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

To do so I tried the following code:

    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")
                }
            }
        }
    }

But whatever I try for 3-4 days now gives me the exact same answer:

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

EDIT:

I downloaded Charles, which gives me the following screen:

enter image description here

What bothers me is the line saying "SSL Proxying not enabled for this host: enable in Proxy Settings, SSL locations" Also, I wasn't able to trigger anything on Charles when entering the curl command via the terminal. I guess it is normal.

Every solution on SO tells me to restart the simulator, which I did. I went to the simulators settings and enabled HTTP Services. I blew on the cartridge and went down to page 2 of Google. Please. Help. Me.

EDIT 2: SOLUTION

Here is the final code, thanks to @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")
 }

Upvotes: 1

Views: 1545

Answers (1)

faarwa
faarwa

Reputation: 146

Send the query parameters via the URL and not the HTTP message body as it's a GET request. Removing:

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

    }

and adding the params to the NSURL (ex. using NSURLComponents) should solve this problem.

Upvotes: 2

Related Questions