我有一个关于通过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
之类的内容附加到该请求中?
答案 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];