Search code examples
iosswifthttpcurlalamofire

How to make this HTTP Post request using alamofire?


How to use alamofire with the given sample?

curl -X POST "http://localhost:2202/api/project" -H "accept: aplication/json" -H "key: David" -H "Content-Type: multipart/form-data" -F "project={"title":"Test Title","description":"Test description for new project","priority":false,"category_id":1,"location_id":1}"


Solution

  • let url = "192.168.1.1/api/project"
    var header = [String:String]()
    header["accept"] = "aplication/json"
    header["key"] = "David"
    let reqParam = ["project":["title":"Test Title","description":"Test description for new project","priority":false,"category_id":1,"location_id":1]]
    Alamofire.upload(multipartFormData: { multipartFormData in
        for (key, value) in reqParam{
            do{
                let data = try JSONSerialization.data(withJSONObject: value, options: .prettyPrinted)
                 multipartFormData.append(data, withName: key)
            }catch(let err){
                print(err.localizedDescription)
            }
        }
    },usingThreshold:UInt64.init(),
      to: url,
      method: .post,
      headers: ["Content-Type": "multipart/form-data"],
      encodingCompletion: { (result) in
    })