使用[formData]发送额外数据 - 目标C.

时间:2014-04-04 02:57:03

标签: ios objective-c afnetworking

我有一个关于通过NSMutableURLRequest通过[formData]发送额外值的快速问题。

更具体地说,这是我的应用程序请求:

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" 
                                                                     path:@"downloadFileRipple.php" 
                                                               parameters:nil 
                                                constructingBodyWithBlock:^(id <AFMultipartFormData>formData)
 {
       [formData appendPartWithFileData:webData 
                                   name:@"file" 
                               fileName:newUsername 
                               mimeType:@"video/quicktime"];
 }];

我的问题是如何将@"link" : videoLink之类的内容附加到该请求中?

1 个答案:

答案 0 :(得分:0)

您可以使用以下方式自行构建请求:

NSString *boundary = @"---###-----##----##--#----###---BOUNDARY---###";
NSMutableData *postBody = [NSMutableData data];
NSString *postString = nil;

postString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
postString = [postString stringByAppendingString:@"Content-Disposition: form-data; name=\"api_password\"\r\n\r\n"];
postString = [postString stringByAppendingString:apiPass];

postString = [postString stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@\r\n", boundary]];
postString = [postString stringByAppendingString:@"Content-Disposition: form-data; name=\"file\"\r\n\r\n"];
postString = [postString stringByAppendingString:filePath];

postString = [postString stringByAppendingString:[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary]];

[postBody appendData:[postString dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://upload.wistia.com"] 
                                                       cachePolicy:NSURLRequestReturnCacheDataElseLoad
                                                   timeoutInterval:60.0f];
[request setHTTPMethod:@"POST"];    

[request setValue:@"*/*" forHTTPHeaderField:@"Accept"];   
[request setValue:[@"multipart/form-data; boundary=" stringByAppendingString:boundary] forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", postBody.length] forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:postBody];