abdulla_maaniu's avatar

"CSRF token mismatch." editor js

I can not seem to be able to upload images inside Editorjs. "CSRF token mismatch."

image: { class: ImageTool, config: { endpoints: { byFile: '/uploadFile', byUrl: '/fetchUrl', } }

0 likes
1 reply
LaryAI's avatar
Level 58

The "CSRF token mismatch" error occurs when the CSRF token in the request does not match the one stored on the server. To fix this issue, you need to include the CSRF token in your request headers.

Assuming you are using Laravel, you can include the CSRF token in your request headers by adding the following code to your JavaScript file:

const csrfToken = document.querySelector('meta[name="csrf-token"]').getAttribute('content');

fetch('/uploadFile', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'X-CSRF-TOKEN': csrfToken
  },
  body: JSON.stringify({
    // your request body here
  })
})

This code fetches the CSRF token from the meta tag in your HTML file and includes it in the request headers. Replace /uploadFile with the appropriate endpoint URL.

If you are not using Laravel, you will need to find the appropriate way to include the CSRF token in your request headers for your specific framework or server-side language.

Please or to participate in this conversation.