swift - How to add Alamofire URL parameters -
i have working scenario using postman passing in url parameters. when try via alamofire in swift, not work.
how create 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
the problem you're using urlencoding.default
. alamofire interprets urlencoding.default
differently depending on http method
you're using.
for get
, head
, , delete
requests, urlencoding.default
encodes parameters query string , adds url, other method (such post
) parameters encoded query string , sent body of http request.
in order use query string in post
request, need change encoding
argument urlencoding(destination: .querystring)
.
you can see more details how alamofire handles request parameters here.
your code should like:
_url = "http://localhost:8080/" let parameters: parameters = [ "test": "123" ] alamofire.request(_url, method: .post, parameters: parameters, encoding: urlencoding(destination: .querystring), headers: headers)
Comments
Post a Comment