ios - send POST request with body dictionary in afnetworking 3.0 -
this question has answer here:
i want ask how send post request body using afnetworking 3.0. appreciated!
from afnetworking's github:
nsmutableurlrequest *request = [[afhttprequestserializer serializer] multipartformrequestwithmethod:@"post" urlstring:@"http://example.com/upload" parameters:nil constructingbodywithblock:^(id<afmultipartformdata> formdata) { [formdata appendpartwithfileurl:[nsurl fileurlwithpath:@"file://path/to/image.jpg"] name:@"file" filename:@"filename.jpg" mimetype:@"image/jpeg" error:nil]; //set request body here } error:nil]; afurlsessionmanager *manager = [[afurlsessionmanager alloc] initwithsessionconfiguration:[nsurlsessionconfiguration defaultsessionconfiguration]]; nsurlsessionuploadtask *uploadtask; uploadtask = [manager uploadtaskwithstreamedrequest:request progress:^(nsprogress * _nonnull uploadprogress) { // not called on main queue. // responsible dispatching main queue ui updates dispatch_async(dispatch_get_main_queue(), ^{ //update progress view [progressview setprogress:uploadprogress.fractioncompleted]; }); } completionhandler:^(nsurlresponse * _nonnull response, id _nullable responseobject, nserror * _nullable error) { if (error) { nslog(@"error: %@", error); } else { nslog(@"%@ %@", response, responseobject); } }]; [uploadtask resume];
edit
the above answer useful if want set custom request body.
if need post simple set of parameters can this:
afhttpsessionmanager *manager = [afhttpsessionmanager manager]; [manager setresponseserializer:[afhttpresponseserializer serializer]]; [manager post:@"http://exaple.com/path" parameters:@{@"param1" : @"foo", @"anotherparameter" : @"bar"} progress:nil success:^(nsurlsessiondatatask * _nonnull task, id _nullable responseobject) { //success block } failure:^(nsurlsessiondatatask * _nullable task, nserror * _nonnull error) { //failure block }];
Comments
Post a Comment