Difference Alamofire quesryString and methodDependent

When use pod alamofire this difference setup `ParameterEncoding` to create serviceApi

 this print in debug area on xcode.


(lldb) po URLEncoding.queryString

▿ URLEncoding

  - destination : Alamofire.URLEncoding.Destination.queryString

  - arrayEncoding : Alamofire.URLEncoding.ArrayEncoding.brackets

  - boolEncoding : Alamofire.URLEncoding.BoolEncoding.numeric



(lldb) po URLEncoding.default

▿ URLEncoding

  - destination : Alamofire.URLEncoding.Destination.methodDependent

  - arrayEncoding : Alamofire.URLEncoding.ArrayEncoding.brackets

  - boolEncoding : Alamofire.URLEncoding.BoolEncoding.numeric



(lldb) po URLEncoding.default.destination

Alamofire.URLEncoding.Destination.methodDependent



(lldb) po URLEncoding.queryString.destination

Alamofire.URLEncoding.Destination.queryString

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

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

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

You can do a simple Alamofire request like this.

import Alamofire
import AlamofireObjectMapper
import ObjectMapper

//
//

Alamofire.request("hello.com", method: .post, parameters: ["name": "alvin"], encoding: JSONEncoding.default, headers: nil).responseObject(completionHandler: { (response : DataResponse<YourModelHere>) in


 })
------------------------------------------------------------

You can see that I have used JSONEncoding in this request. You can choose your way of encoding in there from any of these.

In JSONEncoding

  1. default

    Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding.default)

  2. prettyPrinted

    Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding. prettyPrinted)

And in URLEncoding

  1. default --

    Alamofire.request("https://httpbin.org/get", parameters: parameters, encoding: URLEncoding.default)

  2. methodDependent

    Alamofire.request("https://httpbin.org/get", parameters: parameters, encoding: URLEncoding(destination: .methodDependent))

  3. queryString

  4. httpBody

    Alamofire.request("https://httpbin.org/post", parameters: parameters, encoding: URLEncoding.httpBody)

https://stackoverflow.com/a/43282916/8366535
https://stackoverflow.com/a/39679994/8366535

1 comment:

  1. That was superb information you shared! It was very relatable. I also started my freelancing career six months ago at Eiliana.com. Some ample clients are actually trying hard to Hire a programmer, and I am quite lucky to find a highly interesting project for me.

    ReplyDelete