0

I want to send multiple different JSON object in a single request, i felt streaming the multiple JSON objects like a file in a single request would be better, so kindly let me know if it is possible, if so kindly give me an idea of how to do it using Alamofire, Below is the formate of the raw body (application/json) data that i want to post

{"a":23214,"b":54674,"c":"aa12234","d":4634}
{"a":32214,"b":324674,"c":"as344234","d":23434}
{"a":567214,"b":89674,"c":"sdf54234","d"34634}

I tried the below code, but it didn't work as the body param is not in the correct format, that's the reason i want to try sending the multiple JSON objects as a stream in a single request, Kindly advice

let SendParams = [
      ["a":1234, "b":2345, "c":3456], ["a":2345, "b":3456, "c":4567], ["a":3456, "b":4567, "c":5678]
    ]
_ = Alamofire.request(url, method: .post, parameters: params, encoding: JSONEncoding, headers: header)
5
  • stackoverflow.com/questions/14722669/… Commented Jan 29, 2018 at 7:21
  • The format he is using has a key on the top level json object, where as here it doesn't have a key. That's the reason i am stuck. Commented Jan 29, 2018 at 7:26
  • 1
    @ThooyavanManivaasakar since JSON objects constructed with key,value paris you must have a key to pass the value. Commented Jan 29, 2018 at 12:12
  • I have changed my question as this was what i meant to ask and got confused on my way, kindly try to check out and help me out. Commented Jan 31, 2018 at 6:21
  • check out THIS Commented Oct 17, 2018 at 11:48

1 Answer 1

4

JSON format is not correct for your request, JSON is always key-value pair, where key is always String, and value is any Object. In your example need to set key at top level for object of type Array as below:

let SendParams = [
                    "key" :[["a":1234, "b":2345, "c":3456], ["a":2345, "b":3456, "c":4567], ["a":3456, "b":4567, "c":5678]]
                ]
_ = Alamofire.request(url, method: .post, parameters: SendParams, encoding: JSONEncoding.default, headers: headers).responseJSON { (response) in}

OR

Serialise the array and set as httpBody of URLRequest object:

    let url = YOUR_POST_API_URL
    var request = URLRequest(url: URL(string: url)!)
    request.httpMethod = "POST"
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")

    let values = [
        ["a":1234, "b":2345, "c":3456], ["a":2345, "b":3456, "c":4567], ["a":3456, "b":4567, "c":5678]
    ]
    request.httpBody = try! JSONSerialization.data(withJSONObject: values)

    Alamofire.request(request)
        .responseJSON { response in
            // do whatever you want here
            switch response.result {
            case .failure(let error):
                print(error)

                if let data = response.data, let responseString = String(data: data, encoding: .utf8) {
                    print(responseString)
                }
            case .success(let responseObject):
                print(responseObject)
            }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, i need to send it as multiple JSON objects, not as nested JSON object. So kindly help me in sending multiple JSON objects in body of a Alamofire request or as a stream of multiple JSON Object.
@ThooyavanManivaasakar check the updated answer for jsonserialization body request with array.
Hi, Thanks will try using it and update you on the result. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.