0
votes

I am currently using the following code to post to my server:

[[[RKObjectManager sharedManager] HTTPClient] postPath:[NSString stringWithFormat:@"%@%@", baseURL, @"/api/v2/track-it/rack/individual"]
                                                    parameters:params
                                                       success:^(AFHTTPRequestOperation *operation, id responseObject) {
                                                           // handle success                                                               
                                                           if([[responseObject[@"result"] lowercaseString] isEqualToString:@"success"]){
                                                               // Entry was added
                                                               UIViewController *otherVC = [[UIStoryboard storyboardWithName:@"App" bundle:nil] instantiateViewControllerWithIdentifier:@"Dashboard"];
                                                               [self presentViewController:otherVC animated:YES completion:nil];

                                                               [self alertWithTitle:@"Track It Entry" message:@"The entry was uploaded successfully"];
                                                           } else {
                                                               // Couldn't add entry
                                                               [self alertWithTitle:@"Track It Entry" message:@"An error occured. Saving entry to upload later."];
                                                           }
                                                       }
                                                       failure:^(AFHTTPRequestOperation *operation, NSError *error) {
                                                           // response code is in operation.response.statusCode
                                                           NSLog(@"ERROR");
                                                       }];

params is an NSDictionary of values.

How can I upload files with this?
I can't find any examples anywhere that aren't using a Managed Object.

1

1 Answers

0
votes

On one of my projects I use something like this. Hope it helps.

[[[RKObjectManager sharedManager] HTTPClient] multipartFormRequestWithMethod:RKRequestMethodPOST path:@"somePath" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) {
    NSString *fileName = @"";
    for (UIImage *file in files) {
        fileName = [NSString stringWithFormat:@"%f.jpg", [NSDate timeIntervalSinceReferenceDate]];
        [formData appendPartWithFileData:UIImageJPEGRepresentation(file, .20) name:@"file" fileName:fileName mimeType:@"image/jpeg"];
    }
}];

on the body block, I set a name to the image and a add the image representation to the formData. The .20 is the image quality.