Alamofire: Get request and array params

Created on 3 Feb 2015  ·  3Comments  ·  Source: Alamofire/Alamofire

It doesn't seem like GET requests are translating array params correctly.

    let params: [String: AnyObject] = ["q": ["one", "two"], "target": "en"]
    Alamofire.request(.GET, "https://www.googleapis.com/language/translate/v2", parameters: params)

generates the url:

q%5B%5D=one&q%5B%5D=two&target=en

I think it's trying to added []'s for the POST case. But in the GET case it should be q=one&q=two

All 3 comments

This is intended behavior. Per the README.md,

Since there is no published specification for how to encode collection types, the convention of appending [] to the key for array values (foo[]=1&foo[]=2), and appending the key surrounded by square brackets for nested dictionary values (foo[bar]=baz) [is used].

Instead of using GET, you should consider using POST or PUT and passing values via JSON, XML, or another well-defined format. This could require server side changes obviously.

If server side is out of your control, you should consider manually encoding these parameters instead.

Yea it's the google api endpoint, so it's out of my control. The google API doesn't like that format. I can construct it manually, thought I'd file it though so you guys are aware.

@JRG-Developer is correct—this is intended behavior. Use custom parameter encoding if you need anything other than the provided options.

Was this page helpful?
0 / 5 - 0 ratings