ios - 如何使用 Alamofire 4.0 post 请求快速发送表单数据

标签 ios swift alamofire

我正在尝试发送以下 JSON 数据。我正在使用 Alamofire 4.0。 如何以form-data格式向服务器传递Data?

{
    "apikey" : "455feh54b",
    "action": "ADD",
    "address1" : "Mumbai",
    "country" : "India"
 "userInfo" :     {
        "user_detail" :[
              {
                "name" : "abc",
                "age" : 15,
                "location" : "Delhi"
              },
             {"name" : "pqr",
                "age" : 20,
                "location" : "Mumbai"
             }
          ]
}

}

最佳答案

以下是简单发送的方法

let user_detail = ["name":name, "age":age, "location":location]

let userInfo = ["user_detail": user_detail]

let params = ["apikey":title,
                      "action":type,
                      "address":time,
                      "userInfo":String.JSONStringify(value: user_detail as AnyObject)]

            Alamofire.request(url, method:.post, parameters: params, encoding: URLEncoding.default).validate().responseAuthJSON {
            response in
            switch response.result {
            case .failure(let error):
                print(error)
                self.showAlert(title: "Saving note failed! Please, try again.", message: "")
            case .success(let responseObject):
                print("response is success:  \(responseObject)")

            }
        }

关于ios - 如何使用 Alamofire 4.0 post 请求快速发送表单数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47069419/

相关文章:

ios - 如何在 XCode 6 中添加本地化?

ios - 打开项目时链接器命令失败,退出代码为 1(使用 -v 查看调用)

swift - 迁移到 swift 3 后,Alamofire 对成员 request() 问题的模糊引用

ios - 如何设置navigationItem.titleView图片水平居中?

ios - 无法保存与扩展程序共享的网址

ios - 从 UIScrollview 中删除内容和框架布局指南

ios - UIAlertController 不在不同入口点的层次结构中

ios - POST 请求 Swift 3.0 Alamofire

ios - 如何使用带 token 和参数的 Alamofire 上传图像?

ios - 如何通过命令行 (xcodebuild) 在 Xcode 8 中启用钥匙串(keychain)共享功能