Receiving http request with request in Alamofire in Swift
I am trying to fetch data from mLab service from my mongodb database. I can make request successfully from browser and get data with below code.
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" so I don't get sytax error. The rest is the same. However, it doesn't get anything with Alamofire in Swift on iOS. (I've also tried this without alamofire, with a normal HTTP request, but still doesn't work)
If I try without it {"member_id":2}
, it works. I am sampling with below code (doesn't work);
Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={\"member_id\":2}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI")
I am also trying 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
+3
source to share
1 answer
Your request should look like this:
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 you can also change the parameters easily.
Hope it helps
+4
source to share