NextJS media upload to WP RestAPI not working?

Hello, I would like to upload an image to my WP CMS, but I always get a CORS error message.

Here is a picture of the request:

Here is my code:

  const uploadThumbnailToWordPress = async (base64Image) => {    try {      const response = await fetch(' https://host.innoweso.com/plattform/wp-json/wp/v2/media ', {        method: 'POST',        headers: {          'Content-Type': 'image/png',          'Content-Disposition': 'attachment; filename=thumbnail.png',          'Authorization': `Basic ${encodedCredentials}`,        },        body: base64ToBlob(base64Image), // Convert base64 to Blob      });       if (!response.ok) {        throw new Error('Failed to upload thumbnail to WordPress Media Library');      }       const data = await response.json();      console.log('Thumbnail uploaded successfully:', data.id);      return data.id; // Return attachment ID    } catch (error) {      console.error('Error uploading thumbnail:', error);      return null;    }  };   // Helper to convert base64 to Blob  const base64ToBlob = (base64Data) => {    const byteString = atob(base64Data.split(',')[1]);    const mimeString = base64Data.split(',')[0].split(':')[1].split(';')[0];    const arrayBuffer = new ArrayBuffer(byteString.length);    const intArray = new Uint8Array(arrayBuffer);     for (let i = 0; i < byteString.length; i++) {      intArray[i] = byteString.charCodeAt(i);    }     return new Blob([arrayBuffer], { type: mimeString });  };

Unfortunately, I have no idea what the problem could be because I always use my custom endpoints without any problems and it has always worked.

Thanks in advance for your help!

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
1 Answer
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
GamersGame
3 months ago

Can you share your CORS mistake with us? In there is why it fails