ios - AFNetworking POST Request failed Code=-1011 (400) -


i have webservice , can make post calls via postman utility. on postman settings enter image description here

i using afnetworking. code:

afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; nsdictionary *parameters = @{@"offset": @"1", @"csrf-token": @"63b164ea557b4aba694f81520c71972fa43a9772"}; manager.requestserializer = [afjsonrequestserializer serializer]; [manager post:@"http://simple.com/" parameters:parameters success:^(afhttprequestoperation *operation, id responseobject) {     nslog(@"json: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) {     nslog(@"error: %@", error); }]; 

but error:

error: error domain=com.alamofire.error.serialization.response code=-1011 "request failed: bad request (400)" userinfo=0x7a6c5240 {com.alamofire.serialization.response.error.response=<nshttpurlresponse: 0x7a6b5360> { url: http://simple.com/ } { status code: 400, headers {     "cache-control" = "no-store, no-cache, must-revalidate, post-check=0, pre-check=0, private, must-revalidate";     connection = "keep-alive";     "content-encoding" = gzip;     "content-type" = "text/html; charset=utf-8";     date = "sun, 31 may 2015 15:40:26 gmt";     expires = "thu, 19 nov 1981 08:52:00 gmt";     pragma = "no-cache";     server = "nginx/1.6.2";     "set-cookie" = "csrf-token=5589461e87b28a538953ccac4168bf1f98b20b1ds%3a40%3a%22f52bda5262260a3ee0a10efcf48a6a49c11e4103%22%3b; path=/, phpsessid=803db60816b9f8203fb8feca01c130b6; path=/";     "transfer-encoding" = identity;     vary = "accept-encoding,user-agent";     "x-powered-by" = "php/5.5.17"; 

this without afnetworking:

nsstring *post = @"offset=1&csrf-token=63b164ea557b4aba694f81520c71972fa43a9772"; nsdata *postdata = [post datausingencoding:nsutf8stringencoding allowlossyconversion:yes];  nsstring *postlength = [nsstring stringwithformat:@"%d", [postdata length]];  nsmutableurlrequest *request = [[nsmutableurlrequest alloc] init]; [request seturl:[nsurl urlwithstring:@"http://simple.com/"]]; [request sethttpmethod:@"post"]; [request setvalue:postlength forhttpheaderfield:@"content-length"]; [request setvalue:@"application/x-www-form-urlencoded; charset=utf-8" forhttpheaderfield:@"content-type"]; [request sethttpbody:postdata];   nsurlconnection *connection = [[nsurlconnection alloc] initwithrequest:request delegate:self];  nshttpurlresponse *response = nil; nserror *error = nil; nsdata *respdata = [nsurlconnection sendsynchronousrequest:request returningresponse:&response error:&error]; nslog(@"~~~~~ status code: %d", [response statuscode]);  

and again error: 400

please solve problem

i solved problem.

the fact server keeps session specific csrf token. mistake used same token, in browser, , mobile device. therefore, server , did not give details getting wrong post request.

my solution: first server csrf token, , further post queries use resulting token. in response post request status 200 , data needed.

true afnetworking not in problem, had use standardnsurlconnection (i put code in question). maybe in future come in handy!

thanks all! luck all!


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -