ios - Error when posting header data and x-www-form-urlencoded data -


the data working in postman app not working in below code i.e getting error mentioned @ bottom. cannot understand error in code. in advance.

below screenshots of postman data:

headers:

headers

body: body

below code:

nsmutableurlrequest *request = [nsmutableurlrequest requestwithurl:[nsurl urlwithstring:@"https://apifm.azurewebsites.net/api/checkout/checkoutorders"]];  // specify post request request.httpmethod = @"post";  [request setvalue:@"application/x-www-form-urlencoded" forhttpheaderfield:@"content-type"]; [request setvalue:@"application/json" forhttpheaderfield:@"accept"]; [request setvalue:@"bearer vmueufkfbsafmghchyvvepnn7pwj80rdpsbej7gpvtafq9p6ohdfkepqcei3dippau3rthqntrrqbktaqur4rifiew05dekwhhablvbbbwm8vzx9hgd9asmv-crehqbtknglvvt7nuyxn1lssdorsa4ut0jvxyzezramyglayllet9qwrlkcca1_rm9gj_kzfzoag6vwp70jn0ujahqchpnlb1ape_kjmlxiqwv0ajyaigr4qwsownvyiv8qdhuhvsoaqfpcipkuviyg5fmpkyfletswjptbf5tzhedp_kbg6xaipb7pfjqtbxzyvj7kilac84pubywhogwbivq0vb4x201b9dbwrjfsxkqb0yejd9awqtw8igfuedhifkjycxjskaix4szfs6gaocqdptsob25tseiyammlbis7olbv0ltd6-rsb1q1kfferixszckcdvznpvmdcst1zaq3ug" forhttpheaderfield:@"authorization"];    nsmutabledictionary *postdict = [[nsmutabledictionary alloc] init];  [postdict setvalue:@"39399393" forkey:@"userphone"]; [postdict setvalue:@"manohar" forkey:@"firstname"]; [postdict setvalue:@"adhikari" forkey:@"lastname"]; [postdict setvalue:@"testman@foodmandu.com" forkey:@"email"]; [postdict setvalue:@"9988989800" forkey:@"phone1"]; [postdict setvalue:@"" forkey:@"phone2"]; [postdict setvalue:@"na" forkey:@"organization"]; [postdict setvalue:@"naxal, kathmandu 44600, nepal" forkey:@"address1"]; [postdict setvalue:@"this new address" forkey:@"address2"]; [postdict setvalue:@"1" forkey:@"paymentoption"]; [postdict setvalue:@"delivery" forkey:@"deliverytype"]; [postdict setvalue:@"0" forkey:@"orderid"]; [postdict setvalue:@"128" forkey:@"vendorid"]; [postdict setvalue:@"3108" forkey:@"shippingaddressid"]; [postdict setvalue:@"0" forkey:@"billingaddressid"]; [postdict setvalue:@"here goes special instruction of customer" forkey:@"ordernote"]; [postdict setvalue:@"null" forkey:@"deliverydatetime"]; [postdict setvalue:@"192.168.0.5" forkey:@"customerip"]; [postdict setvalue:@"54" forkey:@"deliverycharge"];   nsdata *jsondata = [nsjsonserialization datawithjsonobject:postdict options:0 error:nil];  // checking format nsstring *urlstring =  [[nsstring alloc] initwithdata:jsondata encoding:nsutf8stringencoding];   // convert data , set request's httpbody property nsstring *stringdata = [[nsstring alloc] initwithformat:@"jsonrequest=%@", urlstring];  nsdata *requestbodydata = [stringdata datausingencoding:nsutf8stringencoding];  request.httpbody = requestbodydata;    [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue mainqueue] completionhandler:^(nsurlresponse *response, nsdata *data, nserror *connectionerror) {     if (!connectionerror) {         nslog(@"response--%@",[[nsstring alloc] initwithdata:data encoding:nsutf8stringencoding]);     } else{         nslog(@"error--%@",connectionerror);     }  }]; 

below error getting:

response--{"message":"an error has occurred.","exceptionmessage":"object reference not set instance of object.","exceptiontype":"system.nullreferenceexception","stacktrace":" @ foodmandu.models.dal.dalcheckout.checkoutorders(jobject jobj, int32 userid)\r\n @ foodmandu.areas.api.checkoutcontroller.checkoutorders(jobject jobj)\r\n @ lambda_method(closure , object , object[] )\r\n @ system.web.http.controllers.reflectedhttpactiondescriptor.actionexecutor.<>c__displayclass10.b__9(object instance, object[] methodparameters)\r\n @ system.web.http.controllers.reflectedhttpactiondescriptor.actionexecutor.execute(object instance, object[] arguments)\r\n @ system.web.http.controllers.reflectedhttpactiondescriptor.executeasync(httpcontrollercontext controllercontext, idictionary`2 arguments, cancellationtoken cancellationtoken)\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.controllers.apicontrolleractioninvoker.d__0.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.controllers.actionfilterresult.d__2.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.filters.authorizationfilterattribute.d__2.movenext()\r\n--- end of stack trace previous location exception thrown ---\r\n @ system.runtime.compilerservices.taskawaiter.throwfornonsuccess(task task)\r\n @ system.runtime.compilerservices.taskawaiter.handlenonsuccessanddebuggernotification(task task)\r\n @ system.web.http.dispatcher.httpcontrollerdispatcher.d__1.movenext()"}

my advice - take afurlrequestserialization dependency, if don't want use afnetworking. create request through it. add afurlrequestserialization.h/m files in project (+ licence file). or self write serialization code.

nsdictionary *postdict = @{             @"userphone" : @"39399393",             @"firstname" : @"manohar",             ....      }; afurlrequestserialization *serializer = [afurlrequestserialization serializer]; nsstring *urlstring = @"https://your_url"; nserror *error = nil; nsmutableurlrequest *request = [serializer requestwithmethod:@"post"                                                                        urlstring:urlstring                                                   parameters:postdict                                                        error:error];  if (error) {   // check error }  // custom http-header values [request setvalue:@"value" forhttpheaderfield:@"key"];  // send request [nsurlconnection sendasynchronousrequest:....]; 


Comments

Popular posts from this blog

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

linux - disk space limitation when creating war file -

How to provide Authorization & Authentication using Asp.net, C#? -