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


Staff member
I'm building a Node.js app which have to create posts on 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
string, but the result is the same.

<a href="$site/posts/new/" rel="nofollow">Here is the Wordpress documentation for this request</a>.

I have no problems to create a post without images.
Also I'm able to create an image post by passing
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 <a href="" rel="nofollow">npm request library</a> for requests).

var localImage = fs.readFileSync('local/path/to/image.jpg');
var anotherImageHostedElsewhere = '';

var url = `${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
}, function(error, response, 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.