Using manager.request with POST
I want to send a request POST
using Alamofire SessionManager
.
I read the documentation at https://github.com/Alamofire/Alamofire/blob/master/Documentation/Alamofire%204.0%20Migration%20Guide.md#parameter-encoding-protocol
but I don't see an example for using query and POST
, download only.
Examples he gives:
let parameters: Parameters = ["foo": "bar"]
Alamofire.request(urlString, parameters: parameters) // Encoding => URLEncoding(destination: .methodDependent)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding(destination: .queryString))
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding(destination: .httpBody))
// Static convenience properties (we'd like to encourage everyone to use this more concise form)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.default)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.queryString)
Alamofire.request(urlString, parameters: parameters, encoding: URLEncoding.httpBody)
My code:
manager.request(url, method: .post, parameters: parameters, encoding: .url)
.responseJSON { response in
fulfill(response)
}
which matches the method signature (from what I can tell), but I get the error "Additional parameter method:
in call.
source to share
You need to match URLRequest
with your parameters in the body likeData
This code can help you
var request = URLRequest(url: urlString!)
request.httpMethod = "POST"
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.httpBody = try! JSONSerialization.data(withJSONObject: parameters)
manager.request(request)
.responseJSON { response in
fulfill(response)
}
Hope it helps
source to share