ios - Error Domain=NSCocoaErrorDomain Code=3840 "Invalid value around character 0." UserInfo={NSDebugDescription=Invalid value around character 0.} -
i trying upload image converting image base64 format. , getting below error.
error domain=nscocoaerrordomain code=3840 "invalid value around character 0." userinfo={nsdebugdescription=invalid value around character 0.}
please refer code
nsdata *imagedata = uiimagepngrepresentation(image); nsstring *imagedatastring = [imagedata base64encodedstring];
here post request method
- (id) postrequest:(nsurl *)posturl poststring:(nsstring *)poststring { nserror * error=nil; nsurlresponse * urlresponse; nsdata *myrequestdata = [ nsdata datawithbytes: [ poststring utf8string ] length: [ poststring length ]]; nsmutableurlrequest * request =[[nsmutableurlrequest alloc]initwithurl:posturl cachepolicy:nsurlrequestreloadignoringcachedata timeoutinterval:10]; [request sethttpbody: myrequestdata]; [request sethttpmethod:@"post"]; [request setvalue:@"application/json; charset=utf-8" forhttpheaderfield:@"content-type"]; nsdata * data =[nsurlconnection sendsynchronousrequest:request returningresponse:&urlresponse error:&error]; if (!data) { return nil; } id jsonnresponse =[nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingallowfragments error:&error]; return jsonnresponse; }
error 3840 ... invalid value around character 0.
simply means (json) string empty, got nothing server.
to nsdata
string there more convenient api:
nsdata *myrequestdata = [poststring datausingencoding:nsutf8stringencoding];
actually base64 formatted string not json specified in header. might cause problem.
ps: don't use synchronous – deprecated – api load data on network
Comments
Post a Comment