ios - API not returning valid response with raw data. but working well on postman -
i trying json response server receive error have pasted below.however api call working fine on postman.please suggest solution doing wrong here
optional(error domain=nsurlerrordomain code=-1012 "the operation couldn’t completed. (nsurlerrordomain error -1012.)" userinfo=0x7f9050e1f750 {nserrorfailingurlstringkey=https://rainforestcloud.com:9445/cgi-bin/post_manager, nsunderlyingerror=0x7f9050fe4eb0 "the operation couldn’t completed. (kcferrordomaincfnetwork error -1012.)", nserrorfailingurlkey=https://rainforestcloud.com:9445/cgi-bin/post_manager})
here snippet of code
var therequest : nsmutableurlrequest = nsmutableurlrequest(url: nsurl(string: base_url)!) therequest.httpmethod = "post" therequest.setvalue("text/html", forhttpheaderfield: "content-type") therequest.addvalue("cloud-id", forhttpheaderfield: "001226") therequest.addvalue("user", forhttpheaderfield: "john.lee@rainforestautomation.com") therequest.addvalue("password", forhttpheaderfield: "rainforest") var stringdata = "confirm_message"; var requestbodydata = stringdata.datausingencoding(nsutf8stringencoding, allowlossyconversion: false) therequest.httpbody = requestbodydata; var response: nsurlresponse? var error: nserror? let urldata = nsurlconnection.sendsynchronousrequest(therequest, returningresponse: &response, error: &error) if ((error) != nil) { println(error) } else { self.performseguewithidentifier("meterview", sender: self) } therequest.timeoutinterval = 0.20
i have code connect rest api. uses jsonvalue, because assume value returned in json.
the way call
datafromserver('file.php', ["parm1": "value1", "parm2": 2]) func datafromserver(file:string, var parms:dictionary<string, anyobject!>) -> jsonvalue { let url = "\(serverurl)/\(file)" let request = nsmutableurlrequest() request.url = nsurl(string: url) request.httpmethod = "post" let boundary = "---------------------------29839217397249927731321937129879827" let contenttype = "multipart/form-data; boundary=\(boundary)" request.addvalue(contenttype, forhttpheaderfield: "content-type") let body = nsmutabledata() (key, value) in parms { if value uiimage { //if uiimage if let img = value as? uiimage { var newwidth = 0 var newheight = 0 var sizelimit = 700 //in px let originalwidth = int(img.size.width) let originalheight = int(img.size.height) //get new size max w/h = 700 if originalwidth > originalheight { //max width newwidth = sizelimit newheight = (originalheight*sizelimit)/originalwidth }else{ newwidth = (originalwidth*sizelimit)/originalheight newheight = sizelimit } let newsize = cgsizemake(cgfloat(newwidth), cgfloat(newheight)) uigraphicsbeginimagecontext(newsize) img.drawinrect(cgrectmake(0, 0, cgfloat(newwidth), cgfloat(newheight))) let newimg = uigraphicsgetimagefromcurrentimagecontext() uigraphicsendimagecontext() let imagedata = uiimagepngrepresentation(newimg) body.appenddata(("\r\n--\(boundary)\r\n" nsstring).datausingencoding(nsutf8stringencoding)!) body.appenddata(("content-disposition: form-data; name=\"\(key)\"; filename=\"tempimage.png\"\r\n" nsstring).datausingencoding(nsutf8stringencoding)!) body.appenddata(("content-type: application/octet-stream\r\n\r\n" nsstring).datausingencoding(nsutf8stringencoding)!) body.appenddata(nsdata(data: imagedata)) } }else{ body.appenddata(("\r\n--\(boundary)\r\n" nsstring).datausingencoding(nsutf8stringencoding)!) body.appenddata(("content-disposition: form-data; name=\"\(key)\";" nsstring).datausingencoding(nsutf8stringencoding)!) body.appenddata(("\r\n\r\n" nsstring).datausingencoding(nsutf8stringencoding)!) body.appenddata(("\(value)" nsstring).datausingencoding(nsutf8stringencoding)!) } } body.appenddata(("\r\n--\(boundary)\r\n" nsstring).datausingencoding(nsutf8stringencoding)!) request.httpbody = body var urldata:nsdata? var response: nsurlresponse? var error: nserror? urldata = nsurlconnection.sendsynchronousrequest(request, returningresponse: &response, error: &error) if let serverdata = urldata { if let data = response { let astr = nsstring(data: serverdata, encoding: nsutf8stringencoding) let json = jsonvalue(urldata) println("return \(file): \(astr)") return json } } return jsonvalue("") }
i hope helps