如何使用AFNetworking 3发送NSData而不使用AFMultipartFormData

时间:2016-04-12 08:13:19

标签: ios objective-c rest afnetworking-3

我正在尝试将wav文件作为NSData发送到AFNetworking 3的休息服务。我想出了如何使用AFMultipartFromData发送,但我得到了一个类似的错误  errorMessage = "Can Not Map Content-Type String multipart/form-data; boundary=Boundary+02588C5 To Media Type ";

当我与创建休息服务的人谈话时,他告诉我,我必须发送NSData而不是AFMultipartFormData。我需要一些帮助,因为我找不到任何方式发送“只”NSData。 我的代码如下;

    NSURL *URL = [NSURL URLWithString:@"http://xxxMyService"];
NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];

configuration.HTTPAdditionalHeaders = @{@"xx": @"yy ; zz"};

AFHTTPSessionManager *manager2 = [[AFHTTPSessionManager alloc] initWithBaseURL:URL sessionConfiguration:configuration];
manager2.responseSerializer = [AFJSONResponseSerializer serializer];

//I converted wav file to NSData
NSData *data=[self setVoiceRecordToNSData];



[manager2 POST:@"http://xxxMyService" parameters:nil
 constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{  
[formData appendPartWithFileData:data name:@"data" fileName:@"Path.wav" mimeType:@"audio/wav"];
}  
progress:nil success:^(NSURLSessionTask *task, id responseObject 
{ NSLog(@"JSON: %@", responseObject);} 

failure:^(NSURLSessionTask *operation, NSError *error) {
         NSLog(@"Error: %@", error); }];

1 个答案:

答案 0 :(得分:0)

尝试在POST之前添加此代码

manager2.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"multipart/form-data"];