1 Reply
      Latest reply: Jan 5, 2017 3:12 AM by eskimo RSS
      mikefromrochester Level 1 Level 1 (0 points)

        I'm trying to download a subset of data from a database on heroku using queryitems with urlcomponents.  However, I always get all the data as if the queryitems are ignored.  What am I missing?  json is used to interact w/ the database.

         

        Code Snippet

                var urlComponents = URLComponents()

                urlComponents.scheme = "http"

                urlComponents.host = "myapp.herokuapp.com"

                urlComponents.path = "/api/v1/hairdata"

         

                let userQuery = URLQueryItem(name:"userid", value:"eagle")

                urlComponents.queryItems = [userQuery]

                var urlRequest = URLRequest(url: urlComponents.url!)

                print("urlrequest is \(urlRequest)")  //this is what prints: urlrequest is http://myapp.herokuapp.com/api/v1/hairdata?userid=eagle


                urlRequest.httpMethod = "GET"

                urlRequest.addValue("Token token=xxx", forHTTPHeaderField: "Authorization")

                print("urlrequest is \(urlRequest)")

            

                let config = URLSessionConfiguration.default

                let session = URLSession(configuration: config)

         

                //make the request

                let task = session.dataTask(with: urlRequest) {

                    (data, response, error) in

                    //check for any errors

                    guard error == nil else {

                        print("error calling GET")

                        print("error=\(error)")

                        return

                    }

          

                    let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)

                    print("responseString = \(responseString)")

         

        Here is what prints:

        responseString = Optional({"hairdata":[{"id":85903,"userid":"honeybee","timedate":"2016-11-28T13:15:37.000Z","deviceid":"151000462","regimen":"eribulin","phase":"CapFit","pumpvoltage":0.0,"railvoltage":12.7136,"scalptemp":0.0,"maintemp":25.31314,"alt1temp":25.52224,"capfitvalue":0.0,"flowrate":0.0},{"id":85904,"userid":"tiger","timedate":"2016-11-28T13:15:38.000Z","deviceid":"151000462","regimen":"eribulin","phase":"CoolDown","pumpvoltage":0.0,"railvoltage":12.7136,"scalptemp":0.0,"maintemp":25.31314,"alt1temp":25.52224,"capfitvalue":0.0,"flowrate":0.0},{"id":85902,"userid":"eagle","timedate":"2016-11-28T13:15:58.000Z","deviceid":"151000462","regimen":"eribulin","phase":"CoolDown","pumpvoltage":0.0,"railvoltage":12.5903,"scalptemp":25.62686,"maintemp":25.52224,"alt1temp":25.62686,"capfitvalue":5.0,"flowrate":0.0},{"id":85901,"userid":"eagle","timedate":"2016-11-28T13:16:18.000Z","deviceid":"151000462","regimen":"eribulin","phase":"CoolDown","pumpvoltage": .....(and lots more)

         

        Why isn't it just the data with eagle as the userid?