0

I'm working on an image upload app. I got the upload part working and would like to include a comment from a UITextField in the POST.

The UITextField is named commentText. myURL is defined in viewDidLoad.

- (IBAction)startUpload {

uploadButton.enabled = NO;
NSData *imageData = UIImageJPEGRepresentation(imageView.image, 0.7);

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:[NSURL URLWithString:myURL]];
[request setHTTPMethod:@"POST"];

//Trying to add comment from uitextfield commentText
NSLog(@"Comment Text: %@", [commentText text]);
NSString *comment = [NSString stringWithFormat:@"comment=%@", [commentText text]];

NSString *boundary = [NSString stringWithString:@"---------------------------94729573838364809882205829479"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request addValue:contentType forHTTPHeaderField:@"Content-Type"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"test.jpg\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];

//for comment
[body appendData:[comment dataUsingEncoding:NSUTF8StringEncoding]];
//[body appendData:[NSData dataWithBytes: [comment UTF8String] length: [comment length]]];

NSLog(@"comment: %@", comment);
[request addValue:[NSString stringWithFormat:@"%i", [body length]] forHTTPHeaderField:@"Content-Length"];

[request setHTTPBody:body];

NSURLResponse *response;
NSError *error;

NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
NSLog(@"responseData: %@", responseData);
}

Much of this code is cobbled together from different posts I've read and I honestly don't know anything about boundaries (yet). I've tried shifting the closing boundary around but that made no difference. Using PHP I've checked $_POST, $_GET and $_REQUEST values but the comment is not there (it would at least show up in $_REQUEST, no?). I know there are other identical questions out there (http://stackoverflow.com/questions/9509419/xcode-easiest-way-to-send-data-from-ios-textfield-for-example-to-a-remote-datab), which I've read, but I can't seem to figure this out.

Thanks,

Mark

EDIT: Does the comment I'm trying to add need to be separated by the boundary again and the content type need to be declared for it? Or something along those lines?

Mark
  • 467
  • 1
  • 6
  • 21

1 Answers1

0

I add various fields along with audio data to my POST, this is how:

  NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@", BASE_URL]];
  NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];

  NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

  NSMutableData *requestData = [NSMutableData data];

  for (NSString *key in [voicenoteData allKeys]) {
    if (![key isEqualToString:@"audio_data"]) {            
      [requestData appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [requestData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"\r\n\r\n", key] dataUsingEncoding:NSUTF8StringEncoding]];
      [requestData appendData:[[NSString stringWithFormat:@"%@\r\n", [voicenoteData objectForKey:key]] dataUsingEncoding:NSUTF8StringEncoding]];
    }
  }

  [requestData appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [requestData appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"audio_file\"; filename=\"recording.m4a\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [requestData appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [requestData appendData:[voicenoteData objectForKey:@"audio_data"]];
  [requestData appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];


  request.HTTPBody = requestData;
8vius
  • 5,786
  • 14
  • 74
  • 136