如何使用alamofire发出此HTTP Post请求?

时间:2019-07-10 07:00:43

标签: ios swift http curl alamofire

如何在给定的样本中使用alamofire?

  

curl -X POST“ http://localhost:2202/api/project” -H“接受:   aplication / json“ -H” key:David“ -H” Content-Type:   multipart / form-data“ -F” project = {“ title”:“测试   标题”,“描述”:“新的测试描述   项目”,“优先级”:false,“类别ID”:1,“位置ID”:1}”

1 个答案:

答案 0 :(得分:0)

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
})