Search code examples
swiftalamofire

Multiple Arguments in AlamoFire.Request


I am trying to access Spotify's web API. I am currently using Alamofire to request the search operation which only requires a token. However, I could not figure out why it won't let me send multiple arguments.

let headers = ["Authorization": "Bearer {your access token}"]
var searchURL = "https://api.spotify.com/v1/search?q=Odesza&type=track"

AF.request(.GET, url, headers: headers)
    .responseJSON { response in
        debugPrint(response)
    }

Solution

  • Alamofire does a great job of formatting parameters to a request url. Just as you pass parameters with the .post method to the AF request function you also pass a parameters [string : any] to the AF request for .get methods as well. The difference is .post will put the parameters in the request.body/data vs. .get will format the parameters into the url with the ?q=my_search_string

    something along the lines of this:

    Let params: [string :any] = ["q": "odesza", "type":"track"] AF.request(.GET, url, parameters:params, headers: headers) .responseJSON { response in debugPrint(response