i know there similar questions, have been stuck on couple of days. had project using asihttprequest perform post , requests against rest server. works fine, since asihttprequest development has been discontinued, planning migrate requests afnetworking. indeed, i'm trying reading server response seems 1 of parameters (gst) not being read.
the asihttprequest (which works fine):
asiformdatarequest *request = [asiformdatarequest requestwithurl:[nsurl urlwithstring:[[nsstring stringwithformat:@"%sexteriorroute/listall", kserverurl] stringbyaddingpercentescapesusingencoding:nsutf8stringencoding]]]; [request addrequestheader:@"content-type" value:@"application/json;charset=utf-8"]; [request addrequestheader:@"cookie" value:[nsstring stringwithformat:@"gst=%@", [user gettoken]]]; [request setrequestmethod:@"post"]; [request setpostbody:[nsmutabledata datawithdata:[[nsstring stringwithformat:@"{\"filters\":\"{\\\"type\\\":\\\"fastfilter\\\",\\\"groups\\\":[{\\\"groupop\\\":\\\"and\\\",\\\"rules\\\":[{\\\"field\\\":\\\"lang\\\",\\\"op\\\":\\\"eq\\\",\\\"data\\\":\\\"%@\\\"}]}],\\\"rules\\\":[],\\\"groupop\\\":\\\"and\\\"}\"}", [user getlang]] datausingencoding:nsutf8stringencoding]]]; [request setdelegate:self]; [request setdidfailselector:@selector(getexteriorroutespreviewfailed:)]; [request setdidfinishselector:@selector(getexteriorroutespreviewfinished:)]; [request startasynchronous]; the afnetworking request (which doesn't work):
nsmutableurlrequest *request = [[afhttprequestserializer serializer] requestwithmethod:@"post" urlstring:[nsstring stringwithformat:@"%sexteriorroute/listall", kserverurl] parameters:[nsdictionary dictionarywithobject:[user gettoken] forkey:@"gst"] error:nil]; [request sethttpshouldhandlecookies:no]; [request setvalue:[nsstring stringwithformat:@"gst=%@", [user gettoken]] forhttpheaderfield:@"cookie"]; nsdata *postbody = [self base64datafromstring:[nsstring stringwithformat:@"{\"filters\":\"{\\\"type\\\":\\\"fastfilter\\\",\\\"groups\\\":[{\\\"groupop\\\":\\\"and\\\",\\\"rules\\\":[{\\\"field\\\":\\\"lang\\\",\\\"op\\\":\\\"eq\\\",\\\"data\\\":\\\"%@\\\"}]}],\\\"rules\\\":[],\\\"groupop\\\":\\\"and\\\"}\"}", [user getlang]]]; [request addvalue:@"application/json;utf-8" forhttpheaderfield:@"content-type"]; [request addvalue:[nsstring stringwithformat:@"%@",[user gettoken]] forhttpheaderfield:@"gst"]; [request sethttpmethod:@"post"]; [request sethttpbody:postbody]; afhttprequestoperation *operation = [[afhttprequestoperation alloc] initwithrequest:request]; [operation setcompletionblockwithsuccess:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", operation.responsestring); }]; [operation start];
have tried
afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; nsdictionary *parameters = @{@"foo": @"bar"}; [manager post:@"http://example.com/resources.json" parameters:parameters success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); }];
Comments
Post a Comment