AFNetworking 2.0。 - multipart-formdata请求未成功

时间:2016-08-10 11:32:18

标签: ios objective-c multipartform-data afnetworking-2

我正在使用AFNetworking 2.0。使用multipart-formdata向服务器发出post请求。我正在尝试上传视频和图像以及3个字符串参数。

这适用于POSTMAN客户端,但不适用于应用程序。

这是确切的代码,它不起作用。

    UIImage *image = img;
    NSData *imageData = UIImagePNGRepresentation(image);

    NSURL *urlVideo = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"SampleVideo_1280x720_1mb" ofType:@"mp4"]];
    NSData *videoData = [NSData dataWithContentsOfURL:urlVideo];

    AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:[NSURL URLWithString:requestURl]];              
    manager.responseSerializer = [AFJSONResponseSerializer serializerWithReadingOptions:NSJSONReadingAllowFragments];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"application/json",@"text/html"]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];

    [manager POST:@"POST" parameters:value constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {

        [formData appendPartWithFileData:imageData name:@"buzzymain_image" fileName:@"photo" mimeType:@"image/png"];            
        [formData appendPartWithFileData:videoData name:@"buzzymain" fileName:@"video" mimeType:@"video/mp4"];            
        [formData appendPartWithFormData:[[value valueForKey:@"buzzymain_category"] dataUsingEncoding:NSUTF8StringEncoding] name:@"buzzymain_category"];
        [formData appendPartWithFormData:[[value valueForKey:@"buzzymain_title"] dataUsingEncoding:NSUTF8StringEncoding] name:@"buzzymain_title"];
        [formData appendPartWithFormData:[[value valueForKey:@"buzzyuser_id"] dataUsingEncoding:NSUTF8StringEncoding] name:@"buzzyuser_id"];

    } success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {                   
        NSString *json = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        NSLog(@"-------\n");
        NSLog(@"%@", json);
        NSLog(@"-------\n");

    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        {
            //Something wrong
        }
    }];

enter image description here

代码错误:

  
    

{&#34; status&#34;:&#34; 0&#34;,&#34; message&#34;:&#34;请输入用户ID。&#34;}

  

POSTMAN成功:

  
    

{&#34; status&#34;:&#34; 1&#34;,&#34; message&#34;:&#34;行动成功。&#34;}

  

P.S。 &#34;值&#34;在上面的代码中是一个NSDictionary。

请告诉我哪里出错?

1 个答案:

答案 0 :(得分:0)

修改

[formData appendPartWithFormData:[[NSMutableData alloc] initWithData:[[NSString stringWithFormat:@"buzzymain_category"] dataUsingEncoding:NSUTF8StringEncoding]] name:@"buzzymain_category"];
[formData appendPartWithFormData:[[NSMutableData alloc] initWithData:[[NSString stringWithFormat:@"buzzymain_title"] dataUsingEncoding:NSUTF8StringEncoding]] name:@"buzzymain_title"];
 [formData appendPartWithFormData:[[NSMutableData alloc] initWithData:[[NSString stringWithFormat:@"buzzyuser_id"] dataUsingEncoding:NSUTF8StringEncoding]] name:@"buzzyuser_id"];