iphone - iOS : How to correctly send put request to server -



iphone - iOS : How to correctly send put request to server -

i have set request trying achieve. problem having isn't sending right raw body on server form/post parameter. expecting raw body {"questions":[{"type":"control_head"}]}, instead getting questions[][type]=control_head tips or suggestions appreciated.

nsstring *jsonstring = @"{\"questions\":[{\"type\":\"control_head\"}]}"; [self createforms:jsonstring]; - (void) createforms : (nsstring *) form { [self executeputrequest:@"user/forms" params:form]; } - (void) executeputrequest : (nsstring *) url params : (nsstring *) params { nsstring *urlstr = [nsstring stringwithformat:@"http://requestb.in/13oujhot"]; afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; nsdata *data = [params datausingencoding:nsutf8stringencoding]; nsdictionary *json = [nsjsonserialization jsonobjectwithdata:data options:0 error:nil]; nsmutabledictionary *userinfo = [[nsmutabledictionary alloc] init]; [manager put:urlstr parameters:json success:^(afhttprequestoperation *operation, id responseobject) { [operation setuserinfo:userinfo]; sbjsonparser *jsonparser = [sbjsonparser new]; id result = [jsonparser objectwithstring:[operation responsestring]]; if ( self.delegate != nil && [self.delegate respondstoselector:finishselector] ) { [self.delegate performselector:finishselector withobject:result]; } } failure:^(afhttprequestoperation *operation, nserror *error) { [operation setuserinfo:userinfo]; if ( self.delegate != nil && [self.delegate respondstoselector:failselector] ) { [self.delegate performselector:failselector withobject:[operation error]]; } }]; }

copied here

updated 1 time again afnetworking 2.0 - see bottom

for afnetworking 1.0:

nsurl *url = [nsurl urlwithstring:@"https://mysite.com/"]; afhttpclient *httpclient = [[afhttpclient alloc] initwithbaseurl:url]; nsdictionary *params = [nsdictionary dictionarywithobjectsandkeys: height, @"user[height]", weight, @"user[weight]", nil]; [httpclient postpath:@"/myobject" parameters:params success:^(afhttprequestoperation *operation, id responseobject) { nsstring *responsestr = [[nsstring alloc] initwithdata:responseobject encoding:nsutf8stringencoding]; nslog(@"request successful, response '%@'", responsestr); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"[httpclient error]: %@", error.localizeddescription); }];

for afnetworking 2.0 (and using new nsdictionary syntax):

afhttprequestoperationmanager *manager = [afhttprequestoperationmanager manager]; nsdictionary *params = @{@"user[height]": height, @"user[weight]": weight}; [manager post:@"https://mysite.com/myobject" parameters:params success:^(afhttprequestoperation *operation, id responseobject) { nslog(@"json: %@", responseobject); } failure:^(afhttprequestoperation *operation, nserror *error) { nslog(@"error: %@", error); }];

ios iphone http afnetworking put

Comments

Popular posts from this blog

xslt - DocBook 5 to PDF transform failing with error: "fo:flow" is missing child elements. Required content model: marker* -

mediawiki - How do I insert tables inside infoboxes on Wikia pages? -

Local Service User Logged into Windows -