Q:

Can't upload image file using Wordpress REST API (Node JS)

I'm building a Node.js app which have to create posts on Wordpress.com using their REST API.
The problem is that Wordpress doesn't accept the image file I'm trying to upload.
This is an error message I've got: 

File is empty. Please upload something more substantial.

I've tried to pass file both as raw Node.js Buffer or as base64 string, but the result is the same.
Here is the Wordpress documentation for this request.
I have no problems to create a post without images. 
Also I'm able to create an image post by passing media_urls param. 
But I fail to upload a local file. So I think the problem is the way I pass raw file data to Wordpress.
Here is how I read file data and make request to Wordpress API (I'm using npm request library for requests).
var localImage = fs.readFileSync('local/path/to/image.jpg');
var anotherImageHostedElsewhere = 'http://example.com/image.jpg';

var url = `https://public-api.wordpress.com/rest/v1.1/sites/${myBlogId}/posts/new`;

var headers = {
  'Content-Type': 'multipart/form-data',
  'Authorization': 'Bearer ' + myAccessToken
}

var formData = {
  title: 'Post title',
  content: 'Post body',
  media: [localImage], //this image fails to upload
  media_urls: [anotherImageHostedElsewhere] //this image is attached ok
}

var requestOptions = {
  url: url,
  headers: headers,
  formData: formData
}

request.post(requestOptions, function(error, response, body) {
  console.log(JSON.parse(body));
});

By the way, I have no problems to upload the same raw file to another APIs (for example Twitter or Imageshack).
Also I have no problems with another requests to Wordpress API.
Really appreciate your help.

A:

You'll want to change the formData of your request to specify media as follows:
var formData = {
  title: 'Post title',
  content: 'Post body',
  'media[0]': fs.createReadStream('local/path/to/image.jpg')
}

Any additional media should increment the index. You can similarly specify attributes by index:
'media_attrs[0][caption]': 'My Great Photo'

