Reputation: 2537
I'm trying to fetch data from mLab service from my mongodb database. I can make the request successfully from browser and get data with code below.
https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={"member_id":2}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI
I need to change "member_id" to \"member_id\" to not to get sytax error. The rest of is same. However, It doesn't fetch anything with Alamofire in Swift in iOS. (I also tried it without alamofire, with usual http request but still doesn't work)
If I try it without {"member_id":2}
It is working.
I'm doing fetching with below code (not working one);
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={\"member_id\":2}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI")
I also try to add parameters
let parameters: Parameters = ["member_id": "3"]
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={\"member_id\":3}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI", parameters: parameters, encoding: URLEncoding(destination: .methodDependent))
This is the api document; http://docs.mlab.com/data-api/
Thank You
Upvotes: 3
Views: 2483
Reputation: 6859
Your request should look like that:
let parameters: Parameters = [
"q": ["member_id": 2],
"apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"
]
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp", method: .get, parameters: parameters, encoding: URLEncoding.default)
It is more readable, and also you can easily change the parameters.
Hope it helps
Upvotes: 4