title: AFNetworking Upload image from iOS to API
body: <p>I'm having trouble uploading an image with data.  This code allows me to upload data just fine:</p>
  
  <pre><code>    NSURL *url = [NSURL URLWithString:@"my_base_url"];
  AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];
  
  //depending on what kind of response you expect.. change it if you expect XML
  [client registerHTTPOperationClass:[AFJSONRequestOperation class]];
  
  NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:
                          _topicText.text,@"Topic",
                          @"1",@"Category",
                          @"",@"MainMedia",
                          @"1",@"Creator",
                          nil];
  [client postPath:@"apidebate/debates" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
      //
  
  } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
      NSLog(@"failure");
  }];
  </code></pre>
  
  <p>However, I've been unsuccessful in trying to upload an image with that data:</p>
  
  <pre><code>    NSURL *url = [NSURL URLWithString:@"my_base_url"];
  AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
  NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"MainMedia"], 0.5);
  NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id &lt;AFMultipartFormData&gt;formData) {
      [formData appendPartWithFileData:imageData name:@"MainMedia" fileName:@"MainMedia" mimeType:@"image/jpeg"];
  }];
  
  AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
  [operation setUploadProgressBlock:^(NSInteger bytesWritten, long long totalBytesWritten, long long totalBytesExpectedToWrite) {
      NSLog(@"Sent %lld of %lld bytes", totalBytesWritten, totalBytesExpectedToWrite);
  }];
  [operation start];
  </code></pre>
  
  <p>What in the work could I be doing wrong??</p>
  
  <p><strong>Edit</strong> (adding server code)</p>
  
  <p>The back-end is Codeigniter with the REST client Phil Sturgeon built (great library, btw).  I'm almost sure it's not the server code though, as the first line of this method sends me an email.  When I attempt to use the above code to post an image, the email never comes.  So, it seems it's nt even getting to the endpoint.</p>
  
  <pre><code>//CONTROLLER FROM API
  function debates_post()
  {
      mail('myemailaddress@gmail.com', 'Test', 'Posted');
      $tmp_dir = "images/posted/";
  
      if(isset($_FILES['MainMedia'])){
          $SaniFileName = preg_replace('/[^a-zA-Z0-9-_\.]/','', basename($_FILES['MainMedia']['name']));
  
          $file = $tmp_dir . $SaniFileName;
          move_uploaded_file($_FILES['MainMedia']['tmp_name'], $file);
      }
      else
          $SaniFileName = NULL;
  
      $data = array('Topic'=&gt;$this-&gt;post('Topic'), 'MainMedia'=&gt;$this-&gt;post('MainMedia'), 'Category'=&gt;$this-&gt;post('Category'), 'Creator'=&gt;$this-&gt;post('Creator'));
      $insert = $this-&gt;debate-&gt;post_debate($this-&gt;post('Topic'), $SaniFileName, $this-&gt;post('Category'), $this-&gt;post('Creator'));
      if($insert){
          $message = $this-&gt;db-&gt;insert_id();
      }
      else{
          $message = 'Insert failed';
      }
  
      $this-&gt;response($message, 200);
  }
  
  //MODEL
  function post_debate($Topic=NULL, $MainMedia='', $Category=NULL, $Creator=NULL){
      $MainMedia = ($MainMedia)?$MainMedia:'';
      $data = array(
                      'Topic' =&gt; $Topic,
                      'MainMedia' =&gt; $MainMedia,
                      'Category' =&gt; $Category,
                      'Creator' =&gt; $Creator,
                      'Created' =&gt; date('Y-m-d h:i:s')
                  );
      return $this-&gt;db-&gt;insert('debate_table', $data);
  }
  </code></pre>