ios - How to use AFNetworking to POST data to Parse.com -
i'm having problem using afnetworking parse.com. i'm trying post data db no luck. i'm not familiar networking stuff , i'm using learning exercise.
i've gotten command work know db set correctly , working posting matter. i'm using afnetworking 2.2.0 post simple test string parse backend.
here code have command works:
-(void)getparsedata{ afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; [manager.requestserializer setvalue:ksdfparseapiapplicationid forhttpheaderfield:@"x-parse-application-id"]; [manager.requestserializer setvalue:ksdfparseapikey forhttpheaderfield:@"x-parse-rest-api-key"]; [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"content-type"]; [manager get:@"https://api.parse.com/1/classes/some_data/" parameters:nil success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); }];
}
and here code have far post command, is't working:
-(void)postparsedata{ afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; [manager.requestserializer setvalue:ksdfparseapiapplicationid forhttpheaderfield:@"x-parse-application-id"]; [manager.requestserializer setvalue:ksdfparseapikey forhttpheaderfield:@"x-parse-rest-api-key"]; [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"content-type"]; manager.responseserializer = [[afjsonresponseserializer serializer]init]; nsdictionary *params = @{@"test_string" : @"nametest"}; [manager post:@"https://api.parse.com/1/classes/warm_data/" parameters: params success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"post data json returned: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); }]; }
i know there custom parse.com api ios exact purpose want use afnetworking learn more , improve. i've tried several different things i'v found on net , stack overflow noting yielded result. here error message returned afhttprequestoperation failure block:
2014-03-24 11:42:07.894 testapp[3222:60b] error: error domain=afnetworkingerrordomain code=-1011 "request failed: bad request (400)" userinfo=0xfe0f400 {nserrorfailingurlkey=https://api.parse.com/1/classes/some_data/, afnetworkingoperationfailingurlresponseerrorkey=<nshttpurlresponse: 0x14780070> { url: api.parse.com/1/classes/some_data/ } { status code: 400, headers { "access-control-allow-origin" = "*"; "access-control-request-method" = "*"; "cache-control" = "no-cache"; connection = "keep-alive"; "content-length" = 130; "content-type" = "application/json; charset=utf-8"; date = "mon, 24 mar 2014 11:42:07 gmt"; server = "nginx/1.4.2"; "set-cookie" = "_parse_session=bah7bkkid3nlc3npb25fawqgogzfriilywqynmfhmta3ztjkzjljyja3mjzkzta1mgqznwiznge%3d--f7812e190b5812d8bba91aea2b12d51412bf84ce; domain=.parse.com; path=/; expires=wed, 23-apr-2014 11:42:07 gmt; secure; httponly"; status = "400 bad request"; "x-runtime" = "0.049574"; "x-ua-compatible" = "ie=edge,chrome=1"; } }, nslocalizeddescription=request failed: bad request (400)}
any , appreciated. if i've forgotten thing or omitted needed fix problem let me know , i'll add can.
after trying suggestions helpful commenters , lots more reading , trying stuff got post working afnetworking! working code follows:
afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; manager.requestserializer = [afjsonrequestserializer serializer]; [manager.requestserializer setvalue:ksdfparseapiapplicationid forhttpheaderfield:@"x-parse-application-id"]; [manager.requestserializer setvalue:ksdfparseapikey forhttpheaderfield:@"x-parse-rest-api-key"]; [manager.requestserializer setvalue:@"application/json" forhttpheaderfield:@"content-type"]; nsdictionary *dic2 = @{@"test_number": @1234}; [manager post:@"https://api.parse.com/1/classes/warm_data" parameters: dic2 success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"post data json returned: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); } ];
this same code before weird. looking through afnetworking docs , other posts talking afnetworking including stackoverflow questions here , came across the
.securitypolicy.allowinvalidcertificates = yes;
part of afhttprequestoperationmanager. added line below manager declaration:
manager.securitypolicy.allowinvalidcertificates = yes;
and started working! set manager.securitypolicy.allowinvalidcertificates no , still worked?!?! removed line complete , keeps working, posting stuff parse me. weird. have no explanation how fixed it, maybe forced parse servers allow when cert removed, again have no idea. i'd love know changed i'm going continue on , build rest of app. if has theories i'd love hear them!
thanks again commenters took time suggest stuff, appreciate it.
Comments
Post a Comment