如何在swift中使用Alamofire 4.0 post请求发送表单数据

时间:2017-11-02 07:02:38

标签: ios swift alamofire

我正在尝试发送以下JSON数据。我正在使用Alamofire 4.0。 如何以表单数据格式将数据传递给服务器?

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

}

4 个答案:

答案 0 :(得分:1)

以下是您可以简单发送的方式

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

            }
        }

答案 1 :(得分:0)

他们已经记录了here
您可以传递如下的JSON数据。

let parameters: Parameters = [
    "foo": [1,2,3],
    "bar": [
        "baz": "qux"
    ]
]

// Both calls are equivalent
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding.default)
Alamofire.request("https://httpbin.org/post", method: .post, parameters: parameters, encoding: JSONEncoding(options: []))

// HTTP body: {"foo": [1, 2, 3], "bar": {"baz": "qux"}}

答案 2 :(得分:0)

let param : Parameters = [
            "id":62393,
            "userName": "Furkan",
            "isActive":true,
            ]
        let header : HTTPHeaders = [Value :"Key"]
        Alamofire.upload(
            multipartFormData: { multipartFormData in
                for (key, value) in Body {
                    multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
                }
        },
        to: urlStr,
        headers: header,
        encodingCompletion: { encodingResult in

            switch encodingResult {
            case .success(let upload, _, _):
                upload.responseString { response in
                          print(response as Any)
                    }
                    .uploadProgress { progress in // main queue by default
                }
                return
            case .failure(let encodingError):

                debugPrint(encodingError)
          }

      })

请尝试使用Alamofire获取表单数据

答案 3 :(得分:0)

 let param : Parameters = [
                "id":62393,
                "userName": "Furkan",
                "isActive":true,
                ]
            let header : HTTPHeaders = [Value :"Key"]
            Alamofire.upload(
                multipartFormData: { multipartFormData in
                    for (key, value) in Body {
                        multipartFormData.append("\(value)".data(using: String.Encoding.utf8)!, withName: key as String)
                    }
            },
            to: urlStr,
            headers: header,
            encodingCompletion: { encodingResult in

                switch encodingResult {
                case .success(let upload, _, _):
                    upload.responseString { response in
                              print(response as Any)
                        }
                        .uploadProgress { progress in // main queue by default
                    }
                    return
                case .failure(let encodingError):

                    debugPrint(encodingError)
              }

          })

请尝试使用Alamofire获取表单数据

相关问题