Next.js Discord

Discord Forum

NextJS API Route returns 413

Unanswered
Cape lion posted this in #help-forum
Open in Discord
Cape lionOP
Hello! I have a prolem and I'm trying to figure out how to fix. I have an api route that generates a PDF and return the data to the client, but I'm receiving a HTTP error code 413 from a file of 4.9MB size. In development mode I can do the call and get the response without problem (receiving a warning on terminal), but when my app is at production I keep receiving the 413 http status code.
I have setted the custom config at pages/api/generate-pdf.ts and this didn't work. The warning is not shown anymore on development mode (about the response size) but I keep not being able to complete the download on client side.

This was my approach until now:
I added this config at pages/api/generate-pdf.ts

export const config = {
  api: {
    responseLimit: false,
    //time in seconds
    maxDuration: 10,
  },
}


and I changed the response to be a stream

res.setHeader('Content-Type', 'application/pdf');
  res.setHeader('Content-Disposition', 'attachment; filename="file.pdf"');

  res.status(200);

  doc.pipe(res, { end: false });
  doc.on('end', () => res.end());

  doc.on('error', (err) => {
    console.error(err);
    res.status(500).end('Internal Server Error');
  });

I tested each approach separeted and together but the result is the, the http code status 413 is still there.

PS: My next app is deployed at AWS Amplify.

4 Replies

4.3MB limit
the config object only work on vercel i think
@Ray the config object only work on vercel i think
Cape lionOP
Oh, I was wondering if the problem reside on AWS Hosting. Maybe you're right, ty!