1

I want to make a request wit Alamofire like this: postman request

As you can see, i have a parameter called "data" and its value is a Json,

How can i do that using Alamofire?

I have tried with parameters, but doesnt wotk

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

Any suggestions?

UPDATE Here is my code

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
Edison Mejia
  • 31
  • 1
  • 4
  • Post your code how are you setting your `parameters` with your array – GIJOW Jul 26 '17 at 19:34
  • check this answer https://stackoverflow.com/questions/44484772/how-to-post-nested-json-by-swiftyjson-and-alamofire/44500753#44500753 @EdisonMejia – Reinier Melian Jul 26 '17 at 19:54

2 Answers2

2

when you have an Any Data as paremeter, you should sent the URLRequest to Alamofire, it supports Any as body

    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
    }
wskcoder
  • 266
  • 2
  • 6
0

Here is an example 4 you, the CURL statement an example of what it is doing.

Note token referenced here is a shared secret, obviously not stuff to post to SO :) bags of print statements in here so that you can see what going on/wrong :)

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)
        }
    })
}
halfer
  • 19,824
  • 17
  • 99
  • 186
user3069232
  • 8,587
  • 7
  • 46
  • 87