0

I am trying to send a base64 encoded image among some other strings using http POST and AFNetworking. I am trying to send the parameters as an NSDictionary:

  NSMutableDictionary *info = [NSMutableDictionary dictionary];
    [info setValue:filedata forKey:@"filedata"];
    [info setValue:comments forKey:@"comments"];
    [info setValue:username forKey:@"username"];
    [info setValue:@"jpg" forKey:@"filetype"];
    [info setValue:filename forKey:@"filename"];

and POST using AFNetworking:

AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    [manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    [manager POST:@"https://myurl.com/fileupload" parameters:info progress:nil success:^(NSURLSessionTask *task, id responseObject) {
        NSLog(@"JSON: %@", responseObject);
    } failure:^(NSURLSessionTask *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];

However I am not sure what else to do at this point. What am I missing? Thank you

McCadi
  • 229
  • 1
  • 2
  • 9

1 Answers1

1

You can use the following example

AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];

[manager POST:@"https://myurl.com/fileupload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData
                                name:@"key name for the image"
                            fileName:photoName mimeType:@"image/jpeg"];
} success:^(NSURLSessionDataTask *task, id responseObject) {
    NSLog(@"Response: %@", responseObject);
} failure:^(NSURLSessionDataTask *task, NSError *error) {
    NSLog(@"Error: %@", error);
}];
casillas
  • 16,351
  • 19
  • 115
  • 215
  • Thank you, however I need to upload all the values. `("username=%@&filename=%@&filetype=jpg&filedata=%@&comments=%@") ` How do I include these parameters? – McCadi Sep 26 '17 at 15:29
  • check this example https://stackoverflow.com/questions/24747809/using-afnetworking-to-post-both-text-and-multiple-images-to-google-blobstore – casillas Sep 26 '17 at 15:34