queryitem isn't working on json path

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=***", 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?

Replies

this is what prints: urlrequest is http://myapp.herokuapp.com/api/v1/hairdata?userid=eagle

This indicates that URLComponents has done its job and created the URL you expected. From there you need to look at why the server is not filtering based on the URL you supplied it. You’ll have to debug this on the server side.

ps You can find a general discussion of issues like this in my Debugging HTTP Server-Side Errors post.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"