0

I have a Post Request, in which I am trying to create an Array of json which the user types and then send to the server, I have used dictionary and it is working for a single request but not for multiple requests.

The JSON structure to be sent is

 {
   "id" : "u_101"
   "data" : [
     { "name" : "Shubham"
       "age" : "23"
     }, 
     {
        "name" : "S"
       "age" : "20"
      }
    ]
}

Here is what I am using in swift for setting the parameters of alamofire request.

 func setData (id: String, data: [Any]) {
   request.httpMethod = post
   var parameters = Parameters()
   parameters["id"] = id
   parameters["data"] = data
 }

Then in the view controller I am doing this, (Items contain a dictionary of entered data through the view )

var allData : [Any] = []
for item in items {
   var data: [String:String] = [:]

       data["name"] = item.key
       data["age"] = item.value
 allData.append(data)
}

setData(id: "u_101", data: alldata)

This is not working and the server is throwing error.

If I send this to the Alamofire post request.

 {
   "id" : "u_101"
   "data" : [
     { "name" : "Shubham"
       "age" : "23"
     }
    ]
}

The server responds with a success.

Shubham
  • 808
  • 8
  • 14
  • What error is the server throwing? – David Chopin Nov 05 '19 at 17:59
  • It's throwing internal server error, I think it's not taking the array of jsons. If there is a single value in items dictionary, it is working. – Shubham Nov 05 '19 at 18:03
  • What error? Also, check show you Alamofire request code. Do something like: `let request = Alamofire.request(...); let body = String(data: request.request!.httpBody!, encoding: .utf8); print("body: \(body))"` and check if it's correct. – Larme Nov 05 '19 at 18:07
  • But does the server ALLOW an array of values for it at least? What's its doc? – Larme Nov 05 '19 at 18:08
  • yeah, the server is expecting an array of jsons under data – Shubham Nov 05 '19 at 18:22
  • can you put your full code regarding sending this request – Ahmad Taalab Nov 06 '19 at 04:22
  • I have added the same can you help me? – Shubham Nov 07 '19 at 17:03
  • Can you make your request work with POSTMAN or curl at least? – Larme Nov 07 '19 at 17:09
  • yeah, Multiple requests are working then, with swift at max I am able to send only single json inside "data" – Shubham Nov 07 '19 at 17:10
  • I'm asking if you are able to have a POSTMAN or curl request with array working. If yes, could you show it? – Larme Nov 07 '19 at 17:14
  • Did youy try to print the "body" as I suggested? To see if the way you give the params is correct? – Larme Nov 07 '19 at 17:15
  • Yeah , I printed the body it looks like this Optional(“data%5B%5D%name%5D=Shubham&data%5B%5D%5Binfo%5D=23&data%5B%5D%name%5D=S&data%5B%5D%age%5D=20&uuid=u_101")) Optional("data%5B%5D%5Bage%5D=23&data%5B%5D%5Bname%5D=Shubham&uuid=u_101”)) – Shubham Nov 07 '19 at 17:45
  • Ok. So you are not sending JSON, you are sending URLEncoded form. Different. Cf. https://stackoverflow.com/questions/9870523/differences-in-application-json-and-application-x-www-form-urlencoded If I remember correctly managing array there is not easy and that's why Alamofire doesn't allow array as direct params. Show the code where you do the Alamofire request. – Larme Nov 08 '19 at 11:03

0 Answers0