AFnetworking multipart request body is nil

前端 未结 1 876
执笔经年
执笔经年 2021-01-19 05:15

I am trying to upload picture to server, via af 2.0 multipart request, but it seems body is always null. Here is a snapshot from code:

[self POST:path parame         


        
1条回答
  •  说谎
    说谎 (楼主)
    2021-01-19 05:37

    You try this code:

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        NSString *strURL = @"http://YOUR_IP:YOUR_PORT/YOUR_PATH/..";
    
        AFHTTPRequestOperationManager *manager     = [AFHTTPRequestOperationManager manager];
        AFJSONRequestSerializer *serializerRequest = [AFJSONRequestSerializer serializer];
        [serializerRequest setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    
        AFJSONResponseSerializer *serializerResponse = [AFJSONResponseSerializer serializer];
        serializerResponse.readingOptions = NSJSONReadingAllowFragments;
        serializerResponse.acceptableContentTypes = [NSSet setWithObject:@"application/json"];
    
        manager.requestSerializer = serializerRequest;
        manager.responseSerializer = serializerResponse;
    
        UIImage *image = [UIImage imageNamed:@"image.png"];
        NSData *imageData = [NSData dataWithData:UIImagePNGRepresentation(image)];
    
        NSDictionary *parameters = @{@"name":@"xxxx",
                                     @"surname":@"xxxx",
                                     };
        [manager POST:strURL parameters:parameters constructingBodyWithBlock:^(id formData) {
            [formData appendPartWithFormData:imageData name:@"photo"];
        } success:^(AFHTTPRequestOperation *operation, id responseObject) {
            NSLog(@""%@,operation.responseString);
        } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
            NSLog(@""%@,operation.responseString);
        }];
    }
    

    0 讨论(0)
提交回复
热议问题