3

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.

5
  • I tested it. It's not working like you said. However I have two points to mention but inconsequential to your dilema. Buffering is not the way to go and guaranteed to fail. the header you are setting is unnecessary as it is added automatically along with a boundary. Commented Nov 5, 2015 at 15:38
  • Hi, thanks for answer. Yeah, the header is unnecessary, I've got it. But what's wrong with buffers? Do you mean I'd better use a stream? Commented Nov 5, 2015 at 16:34
  • A buffer is just data converted into a string and will not work as attachment. Commented Nov 5, 2015 at 17:02
  • Hmm, then I wonder how I managed to pass a buffer to Twitter API and it is uploading it ok. BTW, should I try to pass the attachment as stream? Commented Nov 5, 2015 at 17:06
  • Stream or no stream, it makes no difference. Those nuances are internal to the REST library and have no consequence to the post request itself. Commented Nov 5, 2015 at 17:16

1 Answer 1

3

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'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.