Ackman
Ackman

Reputation: 1592

Alamofire swift4 InvalidURL

Note: Test Api Used

I am using the latest 4.0 version of Alamofire with Swift4.0 and am trying to make a api request (GET) however what I get is a FAIULRE: INvalid URL

func getFlightData(airportCode: String, minutesBehind: String, minutesAhead:String){
    let securityToken: String = "Basic YWFnZTQxNDAxMjgwODYyNDk3NWFiYWNhZjlhNjZjMDRlMWY6ODYyYTk0NTFhYjliNGY1M2EwZWJiOWI2ZWQ1ZjYwOGM="

    var headers: HTTPHeaders = [:]
    headers["Authorization"] = securityToken

    var params: Parameters = [:]
    params["city"] = airportCode
    params["minutesBehind"] = minutesBehind
    params["minuteAhead"] = minutesAhead


    Alamofire.request("https://api.qa.alaskaair.com/1/airports/‬SEA/flights/flightInfo?city=SEA&minutesBehind=10&minutesAhead=120", method: .get, headers: headers).responseJSON { (response) in
        print(response)
    }

The result I get is:

FAILURE: invalidURL("https://api.qa.alaskaair.com/1/airports/‬SEA/flights/flightInfo?city=SEA&minutesBehind=10&minutesAhead=120")

Do i need to encode my url? if yes then how? The same request works great with the URL provided by Alamofire documentation:

Alamofire.request("https://httpbin.org/get")

Upvotes: 1

Views: 1050

Answers (1)

Dule
Dule

Reputation: 499

You need to encode parameters like this:

let parameters: Parameters = ["city": "SEA", "minutesBehind" : "10"] // Add all parameters

Alamofire.request("https://api.qa.alaskaair.com/1/airports/‬SEA/flights/flightInfo", parameters: parameters)

Upvotes: 1

Related Questions