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
default
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding.default)
prettyPrinted
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding. prettyPrinted)
And in URLEncoding
default --
Alamofire.request("https://httpbin.org/get", parameters: parameters, encoding: URLEncoding.default)
methodDependent
Alamofire.request("https://httpbin.org/get", parameters: parameters, encoding: URLEncoding(destination: .methodDependent))
queryString
httpBody
Alamofire.request("https://httpbin.org/post", parameters: parameters, encoding: URLEncoding.httpBody)
https://stackoverflow.com/a/43282916/8366535
https://stackoverflow.com/a/39679994/8366535
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