POST multiple json objects in Alamofire POST method - Swift/IOS

此生再无相见时 提交于 2019-12-01 04:08:42

The correct representation in Swift for the array of comment objects you have posted would be like this:

    let comments: Array<[String:AnyObject]> = [
        [
            "comment": "my First Comment",
            "commentDate": "2014-05-13 14:30 PM",
            "isSigned": 1,
            "patientId": 2,
            "documentId": 3
        ],
        [
            "comment": "my SecondComment",
            "commentDate": "2014-05-14 14:30 PM",
            "isSigned": 2,
            "patientId": 3,
            "documentId": 4
        ],
        [
            "comment": "my third Comment",
            "commentDate": "2014-05-15 14:30 PM",
            "isSigned": 3,
            "patientId": 4,
            "documentId": 5
        ]
    ]

Sending a single comment would be fairly simple:

    let comment: [String:AnyObject] = [
        "comment": "my First Comment",
        "commentDate": "2014-05-13 14:30 PM",
        "isSigned": 1,
        "patientId": 2,
        "documentId": 3
    ]

    Alamofire.request(.POST, "http://httpbin.org/post", parameters: comment).responseJSON { (req, res, json, error) in
        println(req)
        println(res)
        println(json)
        println(error)
    }

However, in order to send an array of comments, seems like you would have to generate the URLRequest your self and then pass it to Alamofire as follows:

    let mutableURLRequest = NSMutableURLRequest(URL: NSURL(string: "http://httpbin.org/post")!)
    mutableURLRequest.HTTPMethod = "POST"
    var error: NSError? = nil

    let options = NSJSONWritingOptions.allZeros
    if let data = NSJSONSerialization.dataWithJSONObject(comments, options: options, error: &error) {
        mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
        mutableURLRequest.HTTPBody = data
    }

    Alamofire.request(mutableURLRequest).responseJSON { (req, res, json, error) in
        println(req)
        println(res)
        println(json)
        println(error)
    }

If you could modify the API backend to accept an object with multiple comments, you could also send them this way:

    Alamofire.request(.POST, "http://httpbin.org/post", parameters: ["comments": comments]).responseJSON { (req, res, json, error) in
        println(req)
        println(res)
        println(json)
        println(error)
    }

Regards.

This would be better

Create dictionary and array of dictionary var Then loop how many parameters you need to send from data source either an array or whatever.

Here my scenario

Need to answer to all questions (will be a random number/size)

var ansParams = [[String: String]]()
var paramz = [String: String]()


for question in sectionQuestions{
    paramz = [
        AppConstants.PARAMETER.KEY_1    : "Value",
        AppConstants.PARAMETER.KEY_2    : "Value",
        AppConstants.PARAMETER.KEY_3    : "Value",
        AppConstants.PARAMETER.KEY_4    : "Value",
        AppConstants.PARAMETER.KEY_5    : "Value"
    ]
    ansParams.append(paramz)
}


print(ansParams)


//Check All Paramz and its values then send ansParams as Parameter to POST request

I had a similar issue in my project while working with an API that did now allow posting multiple objects at once. The formatting of the array as noted above is fine.

   let comments: Array<[String:AnyObject]> = [
        [
            "comment": "my First Comment",
            "commentDate": "2014-05-13 14:30 PM",
            "isSigned": 1,
            "patientId": 2,
            "documentId": 3
        ],
        [
            "comment": "my SecondComment",
            "commentDate": "2014-05-14 14:30 PM",
            "isSigned": 2,
            "patientId": 3,
            "documentId": 4
        ],
        [
            "comment": "my third Comment",
            "commentDate": "2014-05-15 14:30 PM",
            "isSigned": 3,
            "patientId": 4,
            "documentId": 5
        ]
    ]

Then I used a for loop to post each object of the array to post the API.

    var index = comments.count
    var i = 0

    for i = 0; i < index; i++ {

        let urlString = "\(.baseURL)...etc"

        let parameters = comments[i]

        Alamofire.request(.POST, urlString, parameters: parameters)

            .responseJSON { (req, res, data, error) -> Void in

//                    println(req)
//                    println(res)
//                    println(data)
//                    println(error)

                println("\(i) of \(index) posted")
        }
    }

More efficient ways if the API allows, but otherwise this flow works great.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!