如何使用Alamofire发送表单数据体

时间:2017-07-26 19:27:20

标签: ios swift alamofire

我想像Alamofire这样请求: postman request

正如您所看到的,我有一个名为" data"的参数。它的价值是Json,

我怎么能用Alamofire做到这一点?

我尝试过参数,但没有wotk

Alamofire.request(urlservice, method: .post, parameters: ["data": parameters], encoding: JSONEncoding.default, headers: nil).responseJSON { response in

有什么建议吗?

更新 这是我的代码

var arrayProducts = [[String: String]]()

    let product: [String: String] = ["qty": self.txtQty.text!, "precio": self.productPrice, "product_id": self.productId]

    arrayProducts.append(product)

    let parameters = [
        "products": arrayProducts,
        "address": self.userInfo["userAddress"]!,
        "latitude": "6.157738",
        "longitude": "-75.6144665",
        "id": 1,
        "name": self.userInfo["userName"]!,
        "cellphone": self.userInfo["userPhone"]!,
        "emei": "23456resdfty"
        ] as [String : Any]

 Alamofire.request(urlservice, method: .post, parameters: ["data": parameters], encoding: JSONEncoding.default, headers: nil).responseJSON { response in

2 个答案:

答案 0 :(得分:2)

当您将任何数据作为参数时,您应该将URLRequest发送给Alamofire,它支持任何作为正文

    var request = URLRequest(url: URL(string: url)!)

    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    request.httpBody = try! JSONSerialization.data(withJSONObject: parameters, options: [])

    Alamofire.request(request)
        .responseString { (response) in
            // to do anything
    }

答案 1 :(得分:0)

以下是一个示例4,CURL语句是它正在做什么的一个例子。如果它适合你,请勾选绿色勾号!

这里引用的注释令牌是一个共享的秘密,显然不是要发布到SO的东西:)这里的打印语句包,这样你就可以看到发生了什么/ wong :))

func files_download(sourcePath: String) {

    // curl -X POST https://content.dropboxapi.com/2/files/download
    // --header "Authorization: Bearer ab-XXX"
    // --header "Dropbox-API-Arg: {\"path\": \"/acme101/acme1/acme.png\"}"


    var headers:HTTPHeaders!
    let subPart: Dictionary =  ["path":sourcePath]
    do {
        let data = try JSONSerialization.data(withJSONObject: subPart, options: [])
        let dataString = String(data: data, encoding: .utf8)
        headers = ["Authorization": "Bearer " + token, "Dropbox-API-Arg": dataString!]
    } catch {
        print("Oh fudge")
    }

    Alamofire.request("https://content.dropboxapi.com/2/files/download", method: .post, encoding: JSONEncoding.init(options: []), headers: headers).responseData(completionHandler: {feedback in
        guard feedback.result.value != nil else {
        print("Error: did not receive data", //rint("request \(request) feedback \(feedback)"))
        return
        }
        guard feedback.result.error == nil else {
            print("error calling POST on list_folder")
            print(feedback.result.error)
            return
        }

        if let JSON = feedback.result.value {
            print("JSON: \(JSON)")
            let dataString = String(data: JSON, encoding: .utf8)
            print("JSON: \(JSON) \(String(describing: dataString))")
        }
        if let IMAGE = feedback.result.value {
            print("downloaded \(sourcePath) \(sharedDataAccess.currentSN)")
            sharedDataAccess.fnData(index2seek: sharedDataAccess.currentSN, fnData:  feedback.result.value! as Data)
            NotificationCenter.default.post(name: Notification.Name("previewPane"), object: nil, userInfo: nil)
        }
    })
}