title: iPhone Development: Use POST to submit a form
body: <p>I've got the following html form:</p>
  
  <pre><code>&lt;form method="post" action="http://shk.ecomd.de/up.php" enctype="multipart/form-data"&gt;
  &lt;input type="hidden" name="id" value="12345" /&gt;
  &lt;input type="file" name="pic" /&gt;
  &lt;input type="submit" /&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>And the following iPhone SDK Submit method:</p>
  
  <pre><code>- (void)sendfile {
    UIImage *tempImage = [UIImage imageNamed:@"image.jpg"];
  
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setHTTPMethod:@"POST"];
    [request setURL:[NSURL URLWithString:@"http://url..../form.php"]];
  
  
    NSString *boundary = @"------------0xKhTmLbOuNdArY";
    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:NSASCIIStringEncoding]];
    [body appendData:[@"Content-Disposition: form-data; name=\"id\"\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
    [body appendData:[@"12345" dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"\r\n%@\r\n",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
  
    [body appendData:[@"Content-Disposition: form-data; name=\"pic\"; filename=\"photo.png\"\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
    [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSASCIIStringEncoding]];
    [body appendData:[NSData dataWithData:UIImageJPEGRepresentation(tempImage, 90)]];
  
    [body appendData:[[NSString stringWithFormat:@"\r\n%@",boundary] dataUsingEncoding:NSASCIIStringEncoding]];
    // setting the body of the post to the reqeust
    [request setHTTPBody:body];
  
    NSError *error;
    NSURLResponse *response;
    NSData* result = [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];
  
    NSString* aStr = [[[NSString alloc] initWithData:result encoding:NSASCIIStringEncoding] autorelease];
  
    NSLog(@"Result: %@", aStr);
  
    [request release];    
  }
  </code></pre>
  
  <p>This does not work, but I have no clue why. Can you please help me?!! What am I doing wrong?</p>