Search code examples
iosswiftpostmanalamofire

Alamofire .POST encoding (.URL)


I'm trying to do a .POST request with Alamofire with the default option:

    Alamofire.request(.POST, url, parameters: parameters).validate().responseJSON

This one works just fine. As the documentation says, the default encoding for parameters is .URL.

Now I'm trying to improve my code creating routers following this guide: https://grokswift.com/router/

In the end of the implementation is needed to encode the parameters manually. So, if the default was working, I'm using the .URL encoding here now, like so:

    let encoding = Alamofire.ParameterEncoding.URL
    let (encodedRequest, _) = encoding.encode(urlRequest, parameters: parameters)

Now the request won't work, as the parameters are apparently are being sent in the wrong format.

I'm pretty sure the API is functional, I'm testing it in Postman and the parameters are being sent with the form-data option.


Solution

  • If it was working before, double-check that it was using URL encoding:

    let postRequest = Alamofire.request(.POST, url, parameters: parameters)
      .validate()
      .responseJSON { ... }
    debugPrint(postRequest)
    

    That should show you the equivalent cURL statement for your request.

    Then change the router to use .URL encoding and see if debugPrint output changes.