Search code examples
iosswiftalamofire

Missing paramaters value in Alamofire


I'm trying to do a query request in Alamofire, this needs to be correct fetch request link (It is working when try in browser).However, query part comes on the last part of request in Alamofire. It needs to come just after Ctemp. How can I change the order?

https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?q={"value": "50.50"}&apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI

Swift Code;

 let parameters: Parameters = ["q" : ["value" : "50.50"],
               "apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"]


Alamofire.request("https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp", method: .get, parameters: parameters,encoding: URLEncoding.default, headers: nil).responseData{ response in
                    print(response.request)
                    print(response.response)
                    print(response.result)
                    }

response.request which is incorrect and fetches all the datas and don't do query;

Optional(https://api.mlab.com/api/1/databases/mysignal/collections/Ctemp?apiKey=2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI&q%5Bvalue%5D=50.50)

Solution

  • I don't think the parameters in Alamofire are going to treat the nested dictionary properly. It is url encoding those parameters in the url you show as the output. I would suggest changing:

    let parameters: Parameters = ["q" : ["value" : "50.50"],"apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"]
    

    To:

    let parameters: Parameters = ["q" : "{\"value\" : \"50.50\"}", "apiKey": "2ABdhQTy1GAWiwfvsKfJyeZVfrHeloQI"]