swift - Sending json array via Alamofire -


i wonder if it's possible directly send array (not wrapped in dictionary) in post request. apparently parameters parameter should map of: [string: anyobject]? want able send following example json:

[     "06786984572365",     "06644857247565",     "06649998782227" ] 

you can encode json nsjsonserialization , build nsurlrequest yourself. example, in swift 2:

let request = nsmutableurlrequest(url: url) request.httpmethod = "post" request.setvalue("application/json", forhttpheaderfield: "content-type")  let values = ["06786984572365", "06644857247565", "06649998782227"]  request.httpbody = try! nsjsonserialization.datawithjsonobject(values, options: [])  alamofire.request(request)     .responsejson { response in         // whatever want here         switch response.result {         case .failure(let error):             print(error)              // if web service reports error, body of response             // includes description of nature of problem, e.g.              if let data = response.data, let responsestring = string(data: data, encoding: nsutf8stringencoding) {                 print(responsestring)             }         case .success(let responseobject):             print(responseobject)         } } 

or, in swift 3:

var request = urlrequest(url: url) request.httpmethod = "post" request.setvalue("application/json", forhttpheaderfield: "content-type")  let values = ["06786984572365", "06644857247565", "06649998782227"]  request.httpbody = try! jsonserialization.data(withjsonobject: values)  alamofire.request(request)     .responsejson { response in         // whatever 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)         } } 

Comments

Popular posts from this blog

java - Jasper subreport showing only one entry from the JSON data source when embedded in the Title band -

mapreduce - Resource manager does not transit to active state from standby -

serialization - Convert Any type in scala to Array[Byte] and back -