0

I'm trying to make a request to post / upload a new media with WP API v2 (WP 4.7.3). I'm using a Node client (https://github.com/WP-API/node-wpapi) to deal with this.

As I'm only getting errors 400 rest_upload_no_data, I'm trying to find which fields are required to perform this action.

What I'm doing:

wp.media()
        .file( uri )
        .create({
            title: 'My awesome image',
            alt_text: 'an image of something awesome',
            caption: 'This is the caption text',
            description: 'More explanatory information',
            status: 'publish'
        })
        .then(function( response ) {
          ...

I checked the docs (https://developer.wordpress.org/rest-api/reference/media/), but I can't see which param is required or not.

Any help?

enguerranws
  • 8,087
  • 8
  • 49
  • 97

1 Answers1

1

The issue is likely that the .file() method expects a Buffer or local file system path (if running in node) or else the file object from an input field (when running in the browser). A URI string cannot be interpreted as an image, so no image data is sent, causing the "no data" error.

If you are running this library in a browser, you can send an image using the files object of an input:

var data = document.getElementById( 'file-input' ).files[0];
wp.media().file( data )...

The WordPress REST API does not (to my knowledge) support side-loading images, so to upload a remote image you would first have to retrieve it, and then forward that data up to the API yourself.

Edited to include a link to the file upload documentation for the node-wpapi library:

K. Adam
  • 931
  • 5
  • 9
  • I've actually done all of that. Digging further this issue, I'm thinking of superagent issue in React Native environment. I opened an issue there: https://github.com/visionmedia/superagent/issues/1217 – enguerranws May 29 '17 at 07:35