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:
<blockquote>
File is empty. Please upload something more substantial.
</blockquote>
I've tried to pass file both as raw
or as
string, but the result is the same.
<a href="https://developer.wordpress.com/docs/api/1.1/post/sites/$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
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 <a href="https://www.npmjs.com/package/request" rel="nofollow">npm request library</a> for requests).
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.
The problem is that Wordpress doesn't accept the image file I'm trying to upload.
This is an error message I've got:
<blockquote>
File is empty. Please upload something more substantial.
</blockquote>
I've tried to pass file both as raw
Code:
Node.js Buffer
Code:
base64
<a href="https://developer.wordpress.com/docs/api/1.1/post/sites/$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
Code:
media_urls
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="https://www.npmjs.com/package/request" rel="nofollow">npm request library</a> for requests).
Code:
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.