Resolving Unexpected token '<', "<html> <h"... is not valid JSON
"Occasionally, you may encounter this error in applications that require uploading files or making large POST requests. Often, your application is being served by an NGINX server."
Gideon Maina
Senior IoT Engineer
I recently encountered a peculiar error when managing media resources for my Strapi CMS instance. Every time I tried uploading a high definition image or video, I was face with this annoying error Unexpected token '<', "<html> <h"... is not valid JSON.
It is not descriptive enough to give a clear picture where the error might arise from. I thought this was a Strapi issue to do with a broken API or a bug. The most obvious thing to do was look for help on the internet, as you are doing 😁, and specifically for Strapi-related content. This GitHub Issue is a testament to my frustration.
I didn't go far before noting that this was an issue unrelated to Strapi but common for most backend applications. After digging deeper, I realized that the issue came from an NGINX configuration!
By default, NGINX allows a maximum client body size of 1MB. For NGINX to allow you to upload larger files, edit the configuration file in `/etc/nginx/nginx.conf or /etc/nginx/conf.d/ locations and update this line client_max_body_size <desired_size_in_mb>m.
Remember to reload Nginx server sudo systemctl reload nginx.
For example, I increased the maximum allowable image upload size for my Dokku-hosted Strapi app by 45MB by running the following command: dokku nginx:set strapi-app client-max-body-size 45m.
So there you have it. No more issues with file uploads or making large HTTP POST requests.
About Gideon Maina
Senior IoT Engineer
IoT Engineer and Full-stack Developer passionate about building innovative solutions for African challenges. Experienced in sensor networks, web development, and data visualization.