How upload a fileimage via json api?

I’m trying to upload a file or image on a D9 site via json api. The test is namely to upload the user picture on the user entity.

I’ve been following the documentation or read questions about, but I can’t get it working.

The front-end is done with Next.js.

One the test done is the following:

The front-end next calls the next.js api server:

const filename =; const fr = new FileReader(); fr.readAsArrayBuffer(selectedFile); fr.onload = async function () {     const arrayBuffer = fr.result;     if (arrayBuffer && typeof arrayBuffer !== 'string') {         const base64String = _arrayBufferToBase64(arrayBuffer);         const cookieSession = await getUpdateCookie();         const res = await fetch(`/api/users-test/user?cookie=${cookieSession}&name=${filename}`, {             method: 'POST', body: base64String,         });     } }  function _arrayBufferToBase64(buffer: ArrayBuffer) {     let binary = '';     const bytes = new Uint8Array(buffer);     const len = bytes.byteLength;     for (let i = 0; i < len; i++) {         binary += String.fromCharCode(bytes[i]);     }     return window.btoa(binary); } 

The api next.js server post then to Drupal:

const res = await fetch(       `${process.env.NEXT_PUBLIC_DRUPAL_BASE_URL}/jsonapi/user/user/{userID}/user_picture`,       {         method: 'POST',         headers: {           'Content-Type': 'application/octet-stream',           Accept: 'application/vnd.api+json',           'Content-Disposition': `name="file"; filename="${filename}"`,           Authorization: `Bearer ${token}`,         },         body: req.body,       }     );  

I tried different ways to convert the file to "binary file data", but none work: the user picture is always a blank square with an X.

However, if I try the same call with postman, the file is correctly loaded:


So, for now, I hypothesize the problem is on the javascript code, while Drupal has been configured correctly.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

How upload a fileimage via json api?

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.