objective-cpostasiformdatarequest

Server not properly receiving POST request from ASIFormDataRequest in Objective-C


I'm trying to use an ASIFormDataRequest in my iPhone application to send a video I have just recorded to a server, along with a string that I can use to ID who the video belongs to. The code for doing so is here:

-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
    NSURL *urlvideo = [info objectForKey:UIImagePickerControllerMediaURL];
    NSString *urlString=[urlvideo path];
    NSLog(@"urlString=%@",urlString);
    NSString *str = [NSString stringWithFormat:@"http://www.mysite.com/videodata.php"];
    NSURL *url = [NSURL URLWithString:[str stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    NSData *patientData = [_patientCode dataUsingEncoding:NSUTF8StringEncoding];
    [request setFile:urlString forKey:@"video"];
    [request setData:patientData forKey:@"patientcode"];
    [request setRequestMethod:@"POST"];
    [request setDelegate:self];
    [request startSynchronous];
    NSLog(@"responseStatusCode: %i",[request responseStatusCode]);
    NSLog(@"responseString: %@",[request responseString]);
    [picker dismissViewControllerAnimated:YES completion:nil];
}

Everything seems to work correctly, I get back a status code of 200 and the method finishes as expected. However, nothing seems to be received by the php file on the server. I added this line to my server-side php code:

echo(count($_POST));

This returns 0, so it seems as though nothing is actually getting posted by the ASIFormDataRequest. I feel like there might be some simple step I am missing as I have never used the ASIFormDataRequest before, but any help would be greatly appreciated.

EDIT: I changed the setData:patientData to setPostValue:_patientCode and now that part of the post is getting sent correctly, so it seems as though setPostValue works but setData and setFile do not.


Solution

  • I ended up finding the answer while reading something else and noticing that they did something interesting: The file I uploaded was simply going into the $_FILES array, not the $_POST array. I also changed [request setData:patientData forKey:@"patientcode"]; to [request setPostValue:_patientCode forKey:@"patientCode"]; and then that appeared in the $_POST array as I wanted so I was able to get both the string I was sending and the file I was sending in my php script.