front / gutenberg-js

JavaScript only version of the WordPress Gutenberg editor (DEPRECATED)
GNU General Public License v2.0
390 stars 42 forks source link

Uploading media via WP API #27

Closed botoxparty closed 5 years ago

botoxparty commented 5 years ago

So I still can't get it to work with the media route in WP API.

I also still have to force allowing media uploads by calling data.dispatch("core").receiveUploadPermissions(true); after the editor has loaded.

This is the response from the post request to /wp-json/wp/v2/media

code: "rest_upload_no_data"
data: {status: 400}
status: 400
message: "No data supplied."
botoxparty commented 5 years ago

LMAO nevermind I worked it out.

In my apiFetch i was passing options.data for all my POST requests, however the media ones you need to pass options.body in the request.

botoxparty commented 5 years ago

Any ideas on receiving upload permissions on init?

SofiaSousa commented 5 years ago

Hi @botoxparty,

Is your apiFetch expecting requests for OPTIONS wp/v2/media? I'm asking this because it's not so obvious at first sight.

You can check our implementation in g-editor. I hope this can help you.

botoxparty commented 5 years ago

Hey Sofia,

It is but my implementation is slightly different, I actually send the request to WP to fullfill.

if (options.method === "OPTIONS") {
    const { data } = await optionsWordpressData(options.path);
    return new Promise(resolve => {
      resolve(data);
    });

I'll modify this tonight to check options.path and return true if its wp/v2/media

Thanks for your help.

botoxparty commented 5 years ago

Works perfect! Thank you!