title: How to send form-data and x-www-form-urlencoded both in same POST request in ios?
body: <p>I have a problem. I am making POST request to server. In which I am uploading an Image with user_id of User.I need to send user_id in x-www-form-urlencoded and Image in from-data. I tried many ways but every time user_id is undefined in server. How can I send both in same request.
  Here is my <strong>CODE:</strong></p>
  
  <pre><code>   NSString *urlString = [[NSString alloc]initWithString:[NSString stringWithFormat:@"MY URL TO UPLOAD IMAGE "]];
  urlString=[urlString stringByAddingPercentEscapesUsingEncoding:
             NSUTF8StringEncoding];
  NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
  [request setURL:[NSURL URLWithString:urlString]];
  [request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
  [request setHTTPShouldHandleCookies:NO];
  [request setTimeoutInterval:100];
  
  [request setHTTPMethod:@"POST"];
  
  
  
  NSString *boundary = @"------V2ymHFg03ehbqgZCaKO6jy";
  
  
  NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
  [request setValue:contentType forHTTPHeaderField: @"Content-Type"];
  
  [request addValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
  
   NSDictionary *params = @{@"user_id":@"213"};
  NSMutableData *body = [NSMutableData data];
  
   for (NSString *param in params) {
      [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: x-www-form-urlencoded; name=\"%@\"\r\n\r\n", param] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"%@\r\n", [params objectForKey:param]] dataUsingEncoding:NSUTF8StringEncoding]];
  
  }
  
  
  // add image data
  NSData *imageData = UIImageJPEGRepresentation(image, .3);
  if (imageData) {
      [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"my-file\"; filename=\"%@.jpg\"\r\n",@"213"] dataUsingEncoding:NSUTF8StringEncoding]];
  
      [body appendData:[@"Content-Type: image/jpg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:imageData];
      [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
  }
  
  
  
  [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  
  // setting the body of the post to the reqeust
  [request setHTTPBody:body];
  
  NSURLResponse *urlResponse;
  
  NSError *error;
  
  NSData * data=[NSURLConnection sendSynchronousRequest:request returningResponse:&amp;urlResponse error:&amp;error];
  </code></pre>
  
  <p><strong>Output:</strong> data = nil</p>