Search code examples
swiftlyft-api

Ride Estimates Request Error - Bad Parameter (missing required parameter) - Lyft API


I've been using alamofire for my requests, but when I try to make a Ride Estimate Request, I keep getting the same error:

error = "bad_parameter";
"error_detail" =     (
            {
        "start_lat" = "Missing required parameter";
    },
            {
        "start_lng" = "Missing required parameter";
    }
);

My code for the Alamo Requests is:

let headerRequest: HTTPHeaders = ["Authorization" : "bearer <access_token>",
        "Content-Type" : "application/json"]

    let paramsRequest: Parameters = ["start_lat" : 37.7763,
                                     "start_lng" : -122.3918,
                                     "end_lat" : 37.7972,
                                     "end_lng" : -122.453,
                                     "ride_type" : "lyft"]

    Alamofire.request("https://api.lyft.com/v1/cost", method: .get, parameters: paramsRequest, encoding: JSONEncoding.default, headers: headerRequest).responseJSON { response in
        print(response.result.value)
    }

I am unsure how to fix this error. I've been staring at my code for hours, but have not come across a solution. Does anyone have any suggestions or possible solutions to this error?


Solution

  • That's because you're using JSON encoding while Lyft API expects URL encoded parameters. Thus, either remove encoding: JSONEncoding.default or replace it with encoding: URLEncoding.default and, optionally, remove "Content-Type" : "application/json" from headers (if encoding is not set to JSONEncoding, this header has no effect).