Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.5k views
in Technique[技术] by (71.8m points)

ios - Send an array as a parameter in a Alamofire POST request

My app is currently using AWS API Gateway and Alamofire to access different lambda functions that act as my backend.

I have the need to send an array as one of the parameters to one of those API end points, for that I am using the following code:

        var interests : [String]
        interests = globalInterests.map({ (interest) -> String in
            return interest.id!
        })

        // Parameters needed by the API
        let parameters: [String: AnyObject] = [
            "name" : name,
            "lastName" : lastName,
            "interests" : interests
        ]


        // Sends POST request to the AWS API
        Alamofire.request(.POST, url, parameters: parameters, encoding: .JSON).responseJSON { response in

            // Process Response
            switch response.result {

            case .Success:

                print("Sucess")

            case .Failure(let error):
                 print(error)
            }
        }

But that is not working because of the array is not being recognized by the API, but if I create a "static" array

let interests = ["a", "b", "c"] 

Everything works as it is supposed to.

How can I fix this situation given that the array of interests come from another part of the code, how should I declare it or construct it?

A friend managed to accomplish this in Android using an

ArrayList<String>

EDIT:

Printing the parameters array shows me this:

["name":test, "interests": <_TtCs21_SwiftDeferredNSArray 0x7b05ac00>( 103, 651, 42), "lastName": test]
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By using NSJSONSerialization to encode JSON, you can build your own NSURLRequest for using it in Alamofire, here's a Swift 3 example:

    //creates the request        

    var request = URLRequest(url: try! "https://api.website.com/request".asURL())

    //some header examples

    request.httpMethod = "POST"
    request.setValue("Bearer ACCESS_TOKEN_HERE", 
                     forHTTPHeaderField: "Authorization")

    request.setValue("application/json", forHTTPHeaderField: "Accept")

    //parameter array

    let values = ["value1", "value2", "value3"]

    request.httpBody = try! JSONSerialization.data(withJSONObject: values)

    //now just use the request with Alamofire

    Alamofire.request(request).responseJSON { response in

        switch (response.result) {
        case .success:

            //success code here

        case .failure(let error):

            //failure code here
        }
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...