title: How to upload an NSImage to Facebook-Graph Api (through HTTP POST)
body: <p>Very first question here...
  I'm desperately trying to upload a photo to Facebook from my Mac OS App (cocoa), using an HTTP POST request.
  Using the "<a href="https://graph.facebook.com/me/photos?access_token=" rel="nofollow">https://graph.facebook.com/me/photos?access_token=</a>....." followed by "source=MyURLOnline.jpg", it works great, but I need to upload the DATA, not a link of an image already on the Web...</p>
  
  <p>So I'm switching "feed" to "photos", and the URL by the RAW data of my NSImage (and maybe the "source=" to "image=" ?).
  But : I set the header of my request to "multipart/form-data, boundary=%AaB03x" and add some "\r\n" and "Content-Type: image/jpeg\r\n\r\n" etc to the body, but the only thing I get is the error "(#324) Requires upload file"...</p>
  
  <p>I have a few years of experience on Cocoa but don't know anything about HTTP request, and especially what is the Graph API expecting, so I've read all Facebook help I found, would have loved to find an example as I'm sure I make several mistakes, but I'm wondering if it is just possible at all.</p>
  
  <p>ANY help appreciated !</p>
  
  <p>UPDATE:
  Thank you very much Anvesh.
  If you know that I should POST a JSON then I spent my day trying to figure out how to do that, but no success yet.
  Here is my code, if I POST a "source=URLOnline.jpg" to the "feed" (and remove the HTTPHeader and the Body), my image shows on my wall. With my image data to the "photos", the only hint I receive is the #324 error...
  Wondering where I can find out what I should exactly write in the HTTPBody.</p>
  
  <p>// Convert NSImage to data</p>
  
  <pre><code>NSImage * MyIm = [[NSImage alloc] initWithContentsOfURL:[MyLogoPath URL]];
  NSData *imageData = [MyIm TIFFRepresentation];
  NSBitmapImageRep *imageRep = [NSBitmapImageRep imageRepWithData:imageData];
  imageData = [imageRep representationUsingType:NSPNGFileType properties:nil];
  
  
  // HTTP Request with access token
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
  NSString * MyStr = [@"https://graph.facebook.com/me/photos?access_token=" stringByAppendingString:FacebookToken]; // feed?access_token
  
  [request setURL:[NSURL URLWithString:MyStr]];
  
  [request setHTTPMethod:@"POST"];
  
  
  const char *bytes = [[NSString stringWithFormat:@"&amp;image="] UTF8String];
  
  // const char *bytes = [[NSString stringWithFormat:@"&amp;source=http://www.google.ca/intl/en_ALL/images/logos/images_logo_lg.gif"] UTF8String];
  NSMutableData * MyData = [NSMutableData dataWithBytes:bytes length:strlen(bytes)];
  
  
  // HTTP Header
  NSString * boundary = [NSString stringWithFormat:@"AaB03x"];
  NSString * contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
  
  [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
  
  // HTTP Body
  NSMutableData *body = [NSMutableData data];
  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[@"Content-Disposition: attachment; name=\"image\"; filename=\".jpg\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  [body appendData:[@"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]];
  
  [MyData appendData:body];
  [request setHTTPBody:MyData];
  
  [[FacebookView mainFrame] loadRequest:request];
  
  
  //   NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:MyURLRequest delegate:self];
  </code></pre>
  
  <p>//  [connection start];</p>
  
  <p>//    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {</p>
  
  <p>//  }];</p>