Search code examples
jsonswiftswift3alamofire

How to send the given JSON as parameters in the POST request using Alamofire in Swift 3?


I have JSON as follows which has to be included as body for POST request,

    {
      "type" : "myData",
      "condition" : {"projectid": 40}
    }

How should my POST request be formed using Alamofire?


Solution

  • You need to simply create parameter Dictionary.

    let parameters: [String:Any] = [ "type" : "myData", "condition" : ["projectid" : 40]]
    

    Now pass this parameters dictionary with Alamofire.

    Alamofire.request(urlString, method: .post, parameters: parameters, encoding: JSONEncoding.default).responseJSON { response in
        print(response)
    }