<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: dam216</title>
    <description>The latest articles on DEV Community by dam216 (@damiano216).</description>
    <link>https://dev.to/damiano216</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F952897%2Ff020ff85-1ad3-43a5-ab1a-40c7ee051b20.png</url>
      <title>DEV Community: dam216</title>
      <link>https://dev.to/damiano216</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/damiano216"/>
    <language>en</language>
    <item>
      <title>API route doen't read formData in some cases</title>
      <dc:creator>dam216</dc:creator>
      <pubDate>Fri, 02 Dec 2022 14:09:58 +0000</pubDate>
      <link>https://dev.to/damiano216/having-problem-passing-a-formdata-to-api-route-41i6</link>
      <guid>https://dev.to/damiano216/having-problem-passing-a-formdata-to-api-route-41i6</guid>
      <description>&lt;p&gt;I have to pass an uploaded image from the NextJS frontend to the NextJS backend. For some other images, the backend code gets the image and with formidable I am able to read its content. For some other PNG/JPG images of 914 KB however, the backend code doesn't seem to be able to run, since it doesn't print out anything in the terminal.&lt;/p&gt;

&lt;p&gt;Here is my code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//FRONTEND
const form = new FormData();
form.append("file", file);

fetch("/api/myApiRouteFile", {
method: "POST",
body: form,
})
.then((res) =&amp;gt; res.json())
.then((data) =&amp;gt; {
console.log("data", data);
})
.catch((err) =&amp;gt; {
console.log("err", err);
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;//Backend code in NextJS API route
import formidable from "formidable";

export const config = {
api: {
bodyParser: false,
},
};

const handler = async (req, res) =&amp;gt; {
console.log("CODE RUNNING.... 🚀 "); //with some files, this message is not printed out, so I assume the code was not running. With other images, however, it does, so the code is not always running.

const form = new formidable.IncomingForm();
form.uploadDir = "./";
form.keepExtensions = true;
form.parse(req, (err, fields, files) =&amp;gt; {
console.log(err, fields, files); //some images gets printed out, some other no.
});
};
export default handler;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Again, when uploading some&lt;a href="https://dev.tourl"&gt;&lt;/a&gt; images, the backend runs well and prints out the uploaded image file data, but for other images, nothing happens.&lt;/p&gt;

&lt;p&gt;Does anyone know what could be the problem? Maybe a max file size? Thanks for the help.&lt;/p&gt;

&lt;p&gt;With this image the code works well.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Futq609vjxweg9xv9l5qi.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Futq609vjxweg9xv9l5qi.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;With this other image, the API route code is not running&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F785yvf2241ysh9x6ahy4.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F785yvf2241ysh9x6ahy4.jpeg" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Does someone know what may be the problem and solution?&lt;br&gt;
Thanks very much&lt;/p&gt;

</description>
      <category>nextjs</category>
    </item>
  </channel>
</rss>
