title: Unsupported media type when uploading form data to spring server
body: <p>I am trying to upload a file to a remote spring server via an API, and I keep getting an unsupported media type error (415) even though I have already made the data into form data.</p>
  
  <p>Here is the express http post request:</p>
  
  <pre><code>var FormData = require('form-data');
  var fs = require('fs');
  var form = new FormData();
  
  form.append('pid', params.pid);
  form.append('deliveryAttachment', fs.createReadStream(params.deliveryAttachment.path));
  
  var url = someDomain + '/proj/new/deliveryAttachment';
  requestLib({
      url: url,
      method: "POST",
      jar: getJar(),
      form: form
  },function (error, response, body){     
      console.log(body)
  });
  </code></pre>
  
  <p>And here is the Java Spring controller for reference:</p>
  
  <pre><code>    @RequestMapping(value = "proj/new/deliveryAttachment", method = RequestMethod.POST, consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.TEXT_PLAIN_VALUE)
  public String insertDeliveryAttachment(@RequestParam("pid") long pid,
          @RequestParam("deliveryAttachment") MultipartFile file) {
      try {
          DeliveryAttachment a = new DeliveryAttachment(file.getOriginalFilename(), pid);
          ps.insertDeliveryAttachment(a, file.getBytes());
          return String.valueOf(a.id);
      } catch (IOException e) {
          return "-1";
      }
  }
  </code></pre>
  
  <p>This is the form data console log:</p>
  
  <p><a href="https://i.stack.imgur.com/EntYk.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/EntYk.png" alt="enter image description here"></a></p>
  
  <p>And the 415 response: </p>
  
  <pre><code>{
    "timestamp": 1494671395688,
    "status": 415,
    "error": "Unsupported Media Type",
    "exception": "org.springframework.web.HttpMediaTypeNotSupportedException",
    "message": "Content type 'application/x-www-form-urlencoded' not supported",
    "path": "/proj/new/deliveryAttachment"
  }
  </code></pre>
  
  <p><strong>--UPDATE--</strong></p>
  
  <p>Alright, I just found out after reading request's docs that if you use <code>form</code> as the holder for the data, it will treat the data as <code>application/x-www-form-urlencoded</code></p>
  
  <p>e.g.; <code>request.post({url:'http://service.com/upload', form: {key:'value'}}, function(err,httpResponse,body){ ... });</code></p>
  
  <p>Meanwhile the correct key for <code>multipart/form-data</code> is <code>formData</code></p>
  
  <p>e.g.;  <code>request.post({url:'http://service.com/upload', formData: formData}, function optionalCallback(err, httpResponse, body) { ... });</code></p>
  
  <p>I tried it, and now it's giving me a new error:</p>
  
  <p><code>TypeError: Cannot read property 'name' of null at FormData._getContentDisposition</code></p>