title: Uploading Image via POST in Objective C
body: <p>I'm currently working on uploading an image to a server via HTTP Post and can't seem to figure out a way to build the url that calls the service. The user selects an image from the library or camera and then calls a json service that performs the insert statement. </p>
  
  <p>The service is expecting the following uritemplate: </p>
  
  <pre><code>@"%@/DataTransfer/SetUserProfileImage?EMP_ID=%@&amp;image=%@&amp;imageName=%@"
  </code></pre>
  
  <p>It is expecting that the image data is converted somehow to string and sent over url.</p>
  
  <p>This is my current code:</p>
  
  <pre><code>- (BOOL)setUserProfileImage:(UIImage *)imgUser Name:(NSString *)strName{
  
      AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  
      NSData *dataImage = UIImagePNGRepresentation(imgUser);
  
      NSString* theNSString = [[NSString alloc] initWithData:dataImage encoding:NSASCIIStringEncoding];
  
      NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/DataTransfer    /SetUserProfileImage?EMP_ID=%@&amp;"
                                     "image=%@&amp;imageName=%@",
                                     appDelegate.ServerAddress, 
                                     appDelegate.UserId,
                                     theNSString,
                                     strName]];
  
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                                                         cachePolicy: NSURLRequestUseProtocolCachePolicy
                                                     timeoutInterval:10.0];
  
      [request setHTTPMethod:@"POST"];
      [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  
  
      NSURLResponse* response = nil;
     NSError* resultError = nil;
  
     NSData* data = [NSURLConnection sendSynchronousRequest:request 
                                       returningResponse:&amp;response 
                                                   error:&amp;resultError];
  
     NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] 
                                                 length:[data length] 
                                               encoding:NSUTF8StringEncoding]; 
  
     BOOL imgResponse = [strResult boolValue];
     [strResult release];
  
     return imgResponse;
  }
  </code></pre>
  
  <p>I get an error saying that the NSURL is "". Can't seem to build a correct URL. I know that the service itself converts this string to an image again.</p>
  
  <p>UPDATE:</p>
  
  <pre><code>- (BOOL)setUserProfileImage:(UIImage *)imgUser Name:(NSString *)strName{
  
  AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  
  NSString *url = [NSString stringWithFormat:@"%@/DataTransfer/SetUserProfileImage",appDelegate.ServerAddress]; 
  
  NSData *data = UIImagePNGRepresentation(imgUser);
  
  NSString * boundary = @"tweetPhotoBoundaryParm";
  NSMutableData *postData = [NSMutableData dataWithCapacity:[data length] + 1024];
  
  name=\"EMP_ID\"\r\n\r\n%@", @"100-01"];
  NSString * boundaryString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
  NSString * boundaryStringFinal = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];
  
  
  [postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];    
  [postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\";\r\nfilename=\"media.png\"\r\nContent-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  [postData appendData:data];
  [postData appendData:[boundaryStringFinal dataUsingEncoding:NSUTF8StringEncoding]];
  
  NSMutableURLRequest * theRequest=(NSMutableURLRequest*)[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] 
                                                                              cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                                          timeoutInterval:60.0];
  
  [theRequest setHTTPMethod:@"POST"];
  
  [theRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
  //[theRequest addValue:@"www.tweetphoto.com" forHTTPHeaderField:@"Host"];
  NSString * dataLength = [NSString stringWithFormat:@"%d", [postData length]];
  [theRequest addValue:dataLength forHTTPHeaderField:@"Content-Length"];
  [theRequest setHTTPBody:(NSData*)postData];
  
  NSURLConnection * theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
  
  [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
  if (theConnection)
  {
      webData =[[NSMutableData data] retain];
  }
  else
  {
      NSLog(@"%@",@"Could not connect to the network");
  }
  
  return false;
  }
  </code></pre>