Note that there are some explanatory texts on larger screens.

plurals
  1. POPOST with multiple parameters in AFNetworking V2.0
    primarykey
    data
    text
    <p>I am updating some (rather old) ASIHTTPRequest code to AFNetworking V2.0. </p> <p>Now I am dealing with a POST request that uploads an NSDictionary with extra parameters for the request. </p> <p>Here is what the ASIHTTPRequest code looks like: </p> <pre><code>NSMutableData* mPostData = [NSMutableData dataWithData:[postData dataUsingEncoding:NSUTF8StringEncoding]]; NSString *msgLength = [NSString stringWithFormat:@"%d", [postData length]]; [r setPostBody: mPostData]; [r addRequestHeader: @"Content-Length" value:msgLength]; } </code></pre> <p>postData is an NSDictionary that has keys / values - which is based on the action taken. For example - uploading an image will have extra parameters. Completing a user registration will have different parameters - but use the same method this code is found in. </p> <p>The delegate calls this bit of code: </p> <pre><code>//Request must be ASIFormDataRequest - (BOOL) addFileWithPath:(NSString*) filePath fileName: (NSString*)fileName ofType: (NSString*) fileType withKey: (NSString*) fileKey uploadProgressDelegate:(id) uploadProgressDelegate { if ([request isKindOfClass:[ASIFormDataRequest class]]) { ASIFormDataRequest *formRequest = (ASIFormDataRequest *) request; NSLog(@"%@ %@ %@ %@",filePath,fileName,fileType,fileKey); if (uploadProgressDelegate) { [formRequest setUploadProgressDelegate:uploadProgressDelegate]; } NSLog(@"filename = %@",fileName); [formRequest setFile:filePath withFileName:fileName andContentType:fileType forKey:fileKey]; return YES; } else { NSLog(@"WebService must be initialised with PostDataValuesAndKeys so that ASIFormDataRequest is made"); return NO; } } </code></pre> <p>Now from the amount of digging I have done - I can only see this bit of <code>AfNwtworking</code> code - which looks to me like it's from version 1.x </p> <pre><code>NSString *urlString = @"yourUrl"; NSURL* url = [NSURL URLWithString:urlString]; AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url]; NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; NSURL *localVideoURL = [NSURL URLWithString:[userDefaults objectForKey:@"videoURL"]]; NSData *videoData = [NSData dataWithContentsOfURL:localVideoURL]; NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:nil parameters:nil constructingBodyWithBlock: ^(id &lt;AFMultipartFormData&gt;formData) { [formData appendPartWithFileData:videoData name:@"video_file" fileName:@"testvideo.mov" mimeType:@"video/quicktime"]; [formData appendPartWithFormData:[[BAUserInfoParser userInfoJson] dataUsingEncoding:NSUTF8StringEncoding] name:@"userInfo"]; [formData appendPartWithFormData:[[userDefaults objectForKey:@"transactionReceiptData"] dataUsingEncoding:NSUTF8StringEncoding] name:@"transactionData"]; }]; AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request]; [operation setUploadProgressBlock:^(NSUInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) { // NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite); float uploadPercentge = (float)totalBytesWritten / (float)totalBytesExpectedToWrite; float uploadActualPercentage = uploadPercentge *100; [lblUploadInfoText setText:[NSString stringWithFormat:@"%.2f %%",uploadActualPercentage]]; if (uploadActualPercentage &gt;= 100) { lblStatus.text = @"Waitting for response ..."; } progressBar.progress = uploadPercentge; }]; [httpClient enqueueHTTPRequestOperation:operation]; [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { lblStatus.text = @"Upload Complete"; NSData *JSONData = [operation.responseString dataUsingEncoding:NSUTF8StringEncoding]; NSDictionary *jsonObject = [NSJSONSerialization JSONObjectWithData:JSONData options:NSJSONReadingMutableContainers error:nil]; } failure:^(AFHTTPRequestOperation *operation, NSError *error) { NSLog(@"error: %@", operation.responseString); NSLog(@"%@",error); }]; [operation start]; } </code></pre> <p>In version 2 of <code>AFNetworking</code> - there are new convenience methods for this sort of this: </p> <p>Here is the POST method with extra block.. </p> <pre><code>[self.manager POST:url parameters:urlParameters constructingBodyWithBlock:^(id&lt;AFMultipartFormData&gt; formData) { // postData } } success:^(NSURLSessionDataTask *task, id responseObject) { //Success } failure:^(NSURLSessionDataTask *task, NSError *error) { //Failure }]; </code></pre> <p>However its the constructingBodyWithBlock I am having an issue with. I'm not sure what I need to do to take a NSDictionary object and upload it in that block. </p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload