45

I have a working scenario using Postman passing in URL parameters. Now when I try to do it via Alamofire in Swift, it does not work.

How would you create this url in Alamofire? http://localhost:8080/?test=123

    _url = "http://localhost:8080/"
    let parameters: Parameters = [
        "test": "123"
        ]

    Alamofire.request(_url,
                      method: .post,
                      parameters: parameters,
                      encoding: URLEncoding.default,
                      headers: headers
Chris G.
  • 23,930
  • 48
  • 177
  • 302

2 Answers2

121

The problem is that you're using URLEncoding.default. Alamofire interprets URLEncoding.default differently depending on the HTTP method you're using.

For GET, HEAD, and DELETE requests, URLEncoding.default encodes the parameters as a query string and adds it to the URL, but for any other method (such as POST) the parameters get encoded as a query string and sent as the body of the HTTP request.

In order to use a query string in a POST request, you need to change your encoding argument to URLEncoding(destination: .queryString).

You can see more details about how Alamofire handles request parameters here.

Your code should look like:

   _url = "http://localhost:8080/"
    let parameters: Parameters = [
        "test": "123"
        ]

    Alamofire.request(_url,
                      method: .post,
                      parameters: parameters,
                      encoding: URLEncoding(destination: .queryString),
                      headers: headers)
Pedro Castilho
  • 10,174
  • 2
  • 28
  • 39
  • Thank you so much to the OP and to this answer. I've been debugging my code for 3 hours straight! I've been wondering what the f went wrong!!! I always get status 500, was thinking to talk to my backend developer. Good thing I thought of passing the parameters to the URL just for testing, and then I realized that Alamofire doesn't accept parameters when encoding is JSONEnconding.default. – Glenn Posadas Jun 09 '17 at 19:27
  • Many thanks to you. I was struggling with this about why my post request dosen't work. Appreciate you!! – Ravi Oct 05 '18 at 15:15
  • In my parameters I have time in the form date string in the for "2019-08-09T05:30:00".When is passed this a parameter and used URLEncoding(destination: .queryString), my date parameter changed to 2019-08-19T05%3A30%3A00. The above method was the closest creating a right url except for the date parameter. kindly help. – user1899840 Aug 16 '19 at 10:18
  • Such a great answer...I am not really much familiar with alamofire & had no idea why Ino response was received from service as I was getting 400 bad request error....Thanks for great explanation as well... – jayant rawat Sep 15 '20 at 10:51
29

If you want your parameters to be used in querystring, use .queryString as URLEncoding, as in: (I assume you have headers somewhere)

let _url = "http://localhost:8080/"
let parameters: Parameters = [
    "test": "123"
    ]

Alamofire.request(_url,
        method: .post,
        parameters: parameters,
        encoding: URLEncoding.queryString,
        headers: headers)

This form is suggested by Alamofire author because it's more coincise to the other, see screenshot: Excerpt from website

See original here

Andre
  • 1,135
  • 9
  • 20
  • I believe this was how it worked in Swift 2, but it was changed in Swift 3 with the addition of `JSONEncoding` and `CustomEncoding`. – Pedro Castilho Apr 07 '17 at 18:47
  • It's latest Alamofire version using Swift 3. Source here: https://github.com/Alamofire/Alamofire#url-encoding – Andre Apr 07 '17 at 21:09
  • "The Destination enumeration has three cases: ... `.queryString` - Sets or appends encoded query string result to existing query string. ..." As you can see, `.queryString` is a `Destination` value, not an `encoding` value. Encodings are of the form `URLEncoding(destination:)`, `JSONEncoding(destination:)` or other similar types. – Pedro Castilho Apr 10 '17 at 15:42
  • Here you can see the usage in the library example, it's URLEncoding.querystring: https://github.com/Alamofire/Alamofire/blob/144856257feadd8b409f30856a27d3b420506eb3/Tests/ParameterEncodingTests.swift – Andre Apr 10 '17 at 15:48
  • 1
    I edited my answer to add relevant information suggested by Alamofire author, I wouldn't be so sure of the down vote you assigned to me now – Andre Apr 10 '17 at 15:53