0
votes

I am using AFNetworking 2.0. to make a post request with multipart-formdata to the server. I am trying to upload a video and an image along with 3 string parameters.

This is working fine with POSTMAN client but not inside the app.

Here's the exact code which isn't working.

    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

Error with Code:

{"status":"0","message":"Please enter user Id."}

Success with POSTMAN:

{"status":"1","message":"Action Performed Successfully."}

P.S. "value" in above code is a NSDictionary.

Please suggest where I am going wrong?

1
Hi, just to be sure, value does contain a valid user_id associated with the key buzzyuser_id (no typo, no uppercase) ?Florian Burel
"value" contain "buzzyuser_id" ?Ekta Padaliya
@FlorianBurel, thanks, but there's no type or any other cases, I have checked everything for many times. In postman it works properly with the same params.Hemang
@EktaMakadiya, Yes, it contains it. I have checked that it contains all the requires params.Hemang
@Hemang Please check my answer.Ekta Padaliya

1 Answers

0
votes

Edit:

[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"];