title: iOS - Unable to successfully post Multipart form Data to server using HTTP Post request
body: <p>I am developing an iPhone app where I need to pass on the data from the mobile to the server. The data is in the form of multipart form data.</p>
  
  <p>unfortunately, the form data is not hitting the server. I tried all the ways. But I am not able to find the issue in my code.  I am very sure the issue is in the client side as I am albe to hit API endpoint in the server from the Android app</p>
  
  <p>The code is below</p>
  
  <pre><code>NSString *str;
                  str=[NSString stringWithFormat:@"https://www.mysite.com/ttk/rest/detail/updat‌​edetail"];
  
                  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  
                  [request setURL:[NSURL URLWithString:str]];
                  [request setHTTPMethod:@"POST"];
  
                  NSMutableData *body = [NSMutableData data];
  
                  NSString *boundary = @"---------------------------Boundary_1_511262261_1369143433608";
  
                  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
                  [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
  
  
                  //  parameter imageData
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[@"Content-Disposition: form-data; name=\"profileimg\"; filename=\"a.png\"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
                  NSData *imageData=UIImagePNGRepresentation(pickedImage);
  
                  [body appendData:[NSData dataWithData:imageData]];
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
                  //  parameter mobilenumber
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mobileNumber\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  
                  [body appendData:[mobilenumber dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
                  //  parameter countryCode
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"countryCode\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  
                  [body appendData:[countrycode dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
  
                  //  parameter displayName
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"displayName\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  
                  [body appendData:[strusername dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
  
                  //  parameter mobileID
  
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"mobileID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  
                  NSString *strsa3=[NSString stringWithFormat:@"%@",mobileID];
                  [body appendData:[strsa3 dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
                  //  parameter emailid
  
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"emailid\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  
                  [body appendData:[stremail dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
                  //  parameter Boolean
  
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"onlycontacts\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[searchwithin dataUsingEncoding:NSUTF8StringEncoding]];
  
  
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
  
                  //  parameter searchby
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"searchby\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
                  if(Radio1Check)
                  {//Context
                      [body appendData:[@"Context" dataUsingEncoding:NSUTF8StringEncoding]];
                  }
                  else
                  {
                      [body appendData:[@"Location" dataUsingEncoding:NSUTF8StringEncoding]];
                  }
  
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
                  //     parameter show location to other
  
                  [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"showlocation\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  
                  [body appendData:[location dataUsingEncoding:NSUTF8StringEncoding]];
                  [body appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
  
  
                  // close form
                  [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  
  
                  [request setHTTPBody:body];
  
                  NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
                  NSString *json=[[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
                  NSLog(@"Resp string: %@",json);
  </code></pre>
  
  <p>I have set the closing boundary and tested this code many times. But the issue seems to be  hiding somewhere in the code which I am not able to find.</p>
  
  <p>The returndata from the server is always Nil and is not hitting the server.. I have double checked the URL and the URL is also correct. Please help me in resolving this issue. Thanks for your time</p>