ios - Set HTTP Body to AFNetworking instead of using native request -


ive been trying afnetworking work did native request, have been using afnetworking uploading post or get.but never uploaded image correct. uploaded server damaged , i've explained issue in reference : uploading image server reached damaged using afnetworking

i've tried use native nsurlconnection.sendsynchronousrequest http body , has been uploaded , not damaged. , code work need in afnetworking because supports ios 8 + , more reasons.

        let baseurl = ""         let selectedimage = self.userimage.image         let imagedata = nsdata(data: uiimagejpegrepresentation(selectedimage!, 0.5)!)          let request = nsmutableurlrequest()         request.url = nsurl(string: baseurl)         request.httpmethod = "post"         request.addvalue("image/jpeg", forhttpheaderfield: "content-type")         let body = nsmutabledata(data: imagedata)         request.httpbody = body   {             let responsedata = try nsurlconnection.sendsynchronousrequest(request, returningresponse:nil)             let responsestring = string(data: responsedata, encoding: nsutf8stringencoding)              print("user image uploaded navigating home services")           } catch (let error nserror) { } 

following solution in objective-c, replicate same process swift too. create object afhttprequestoperationmanager shown below.

 [[afhttprequestoperationmanager alloc] initwithbaseurl:@"www.google.com"]; 

now define nsdictionary key value pairs parameters request body.

nsdictionary *parameters = @{                              @"paramaname": value,                              @"paramaname":value                         }; 

now , when calling post request

 [self.manager post:@"somemethodname" parameters:parameters success:^(afhttprequestoperation *operation, id responseobject) {     nsdictionary * responsedict = (nsdictionary *)responseobject;     nslog(@"responsedict : %@",responsedict); } failure:^(afhttprequestoperation *operation, nserror *error) {     nslog(@"error  : %@ code %d",error.localizeddescription,error.code);     } }]; 

this how can send post request parameters using afnetworking.


Comments

Popular posts from this blog

SVG stroke-linecap doesn't work for circles in Firefox? -

routes - Laravel 4 Wildcard Routing to Different Controllers -

cross browser - XSLT namespace-alias Not Working in Firefox or Chrome -