Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add Alamofire URL parameters

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 
like image 215
Chris G. Avatar asked Apr 07 '17 15:04

Chris G.


People also ask

How do I pass Alamofire header in Swift?

For headers that change from request to request, you can pass them directly to the request method. From the docs: Adding a custom HTTP header to a Request is supported directly in the global request method. This makes it easy to attach HTTP headers to a Request that can be constantly changing.

Is Alamofire an asynchronous?

Networking in Alamofire is done asynchronously. Asynchronous programming may be a source of frustration to programmers unfamiliar with the concept, but there are very good reasons for doing it this way.

What is URLRequestConvertible?

The URLRequestConvertible protocol is a lightweight way to ensure a given object can create a valid NSURLRequest . There's not really a strict set of rules or guidelines that exist forcing you to use this protocol in any particular way.


2 Answers

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) 
like image 119
Pedro Castilho Avatar answered Nov 20 '22 12:11

Pedro Castilho


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

like image 45
The Horny Coder Avatar answered Nov 20 '22 12:11

The Horny Coder