I have a simple put request and I am using Alamofire\'s Parameters
type to send data to the server. I would like to use codable. How do I either convert my coda
You can make a new URLRequest
and set httpBody
to your encoded jsonData
. Try this code...
func addProduct(product: MainProduct, completionHandler: @escaping ((JSON?, Error?)->Void)) {
let encoder = JSONEncoder()
let jsonData = try! encoder.encode(product)
let url = "INSERT_URL"
var request = URLRequest(url: url)
request.httpMethod = HTTPMethod.put.rawValue
request.setValue("application/json; charset=UTF-8", forHTTPHeaderField: "Content-Type")
request.httpBody = jsonData
Alamofire.request(request).responseJSON { response in
switch response.result {
case .success(let value):
print ("finish")
let swiftyJson = JSON(value)
completionHandler(swiftyJson, nil)
case .failure(let error):
completionHandler(nil, error)
}
}
}