title: Error: Unexpected end of multipart data] storageErrors: [] in express.js-iOS (Objective-C) app
body: <p>I've tried to find solution of this problem, couldn't find anything in node.js/express.js and iOS app. My client has requirements not to use any third party libraries. So, I am not using AFNetworking. Any help or if someone can just tell why the error is coming would be greatly appreciated.</p>
  
  <p>I am trying to send multipart form data, image with text. Whenever i do POST i get error as: Unexpected end of multipart data.</p>
  
  <p>Code for POST in iOS:</p>
  
  <pre><code>NSURL *url = [NSURL URLWithString:@"http://172.16.0.5:8080/upload"];
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
      [request setHTTPMethod:@"POST"];
  
      NSString *boundary = @"YOUR_BOUNDARY_STRING";
      NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
      [request addValue:contentType forHTTPHeaderField:@"Content-Type"];
  
      NSMutableData *body = [NSMutableData data];
  
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"photo\"; filename=\"%@.jpg\"\r\n", self.path] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[NSData dataWithData:self.imageData]];
  
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"message\"\r\n\r\n%@", self.mealTitleTextField.text] dataUsingEncoding:NSUTF8StringEncoding]];
  
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
      [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"user\"\r\n\r\n%d", 1] dataUsingEncoding:NSUTF8StringEncoding]];
  
      [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
  
      [request setHTTPBody:body];
  
      NSURLResponse *response;
      NSError *error;
  
      [NSURLConnection sendSynchronousRequest:request returningResponse:&amp;response error:&amp;error];
  </code></pre>
  
  <p>This is how I am getting image file path:</p>
  
  <pre><code> NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
                                                           NSUserDomainMask, YES);
      NSString *documentsDirectory = [paths objectAtIndex:0];
      self.path = [documentsDirectory stringByAppendingPathComponent:
                        @"test.png" ];
      self.imageData = UIImagePNGRepresentation(image);
      [self.imageData writeToFile:self.path atomically:YES];
  </code></pre>
  
  <p>Here is the server code:</p>
  
  <pre><code>router.post('/upload', function(req,res,next){
      upload.single('photo')(req, res, function(err){
          if (err) {
              console.log(err)
              res.send(err);
              return;
          }
  
          console.log(req.file);
          res.json({'ok':'ok'});
      });
  });``
  </code></pre>