Skip to content

Instantly share code, notes, and snippets.

@Maqsim
Last active January 21, 2025 12:10
Show Gist options
  • Select an option

  • Save Maqsim/857a14a4909607be13d6810540d1b04f to your computer and use it in GitHub Desktop.

Select an option

Save Maqsim/857a14a4909607be13d6810540d1b04f to your computer and use it in GitHub Desktop.
HOW TO FIX "413 Payload too large" in NodeJS (Express)
const express = require('express');
const bodyParser = require('body-parser');
...
// Express 4.0
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb' }));
// Express 3.0
app.use(express.json({ limit: '10mb' }));
app.use(express.urlencoded({ limit: '10mb' }));
...
@united-forever

Copy link
Copy Markdown

Thanks bro!

@Mea-amor

Copy link
Copy Markdown

Thanks for that code my friend, you help me

@mlockett42

mlockett42 commented Jan 15, 2022

Copy link
Copy Markdown

@manonironside One potential downside is if untrusted users can submit to this end point it can be used by attackers to run denial of service attacks that exhaust the server's available memory

@KirillGoorevich

Copy link
Copy Markdown

I had this in my code:
app.use(express.json());
and I couldn't figure out why it didn't work then I tried this specific order:
const bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb' }));
app.use(express.json());
and now it works.
why?

@tepaseointenso

Copy link
Copy Markdown

I had this in my code:
app.use(express.json());
and I couldn't figure out why it didn't work then I tried this specific order:
const bodyParser = require('body-parser');
app.use(bodyParser.json({ limit: '10mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '10mb' }));
app.use(express.json());
and now it works.
why?

i have the same problem

@ilorwork

Copy link
Copy Markdown

FYI:
you have to put those lines before
app.use(express.json());

ghost commented Nov 6, 2022

Copy link
Copy Markdown

Thank For sharing this

@DanielKomlaElijah

Copy link
Copy Markdown

This also worked for me. Thanks for sharing....

@milton-imageline

Copy link
Copy Markdown

+1

@Jsonlm

Jsonlm commented May 8, 2023

Copy link
Copy Markdown

Thanks a lot your answer helps a lot!!

@SwatiRanjangit

Copy link
Copy Markdown

Thanks a lot..now i'm able to upload it.

@QuocZuong

Copy link
Copy Markdown

+1 Siuuuuuuuuu

@danieltroger

Copy link
Copy Markdown

Thanks!

@zaadevofc

Copy link
Copy Markdown

not work :(

error:

413: PAYLOAD_TOO_LARGE
Code: FUNCTION_PAYLOAD_TOO_LARGE

this my code :

app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '100mb' }));
app.use(express.json());

@Shahtaymur

Copy link
Copy Markdown

FYI: you have to put those lines before app.use(express.json());

this worked for me.. 👍

@Rahad23

Rahad23 commented Mar 18, 2024

Copy link
Copy Markdown

not work :(

error:

413: PAYLOAD_TOO_LARGE
Code: FUNCTION_PAYLOAD_TOO_LARGE

this my code :

app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '100mb' }));
app.use(express.json());

How did you solve later?

@Elphaid

Elphaid commented Apr 30, 2024

Copy link
Copy Markdown

FYI: you have to put those lines before app.use(express.json());

It works

Thank you

@devlopersabbir

devlopersabbir commented May 10, 2024

Copy link
Copy Markdown

Why it's still says payload is too large??

app.use(bodyParser.json({ limit: "1024mb" }));
app.use(bodyParser.urlencoded({ extended: true, limit: "1024mb" }));
app.use(express.json({ limit: "1024mb" }));

Note: I'm using Quill Rich Editor and uploading images.

@sachinpal293

Copy link
Copy Markdown

thank u it works properly

@Charmnut

Copy link
Copy Markdown

not work :(

error:

413: PAYLOAD_TOO_LARGE
Code: FUNCTION_PAYLOAD_TOO_LARGE

this my code :

app.use(bodyParser.json({ limit: '100mb' }));
app.use(bodyParser.urlencoded({ extended: true, limit: '100mb' }));
app.use(express.json());

same issue, trouble me much, anyone know the reason about it?

@rainb3rry

Copy link
Copy Markdown

goodd

@hitecSmartHome

hitecSmartHome commented Jan 21, 2025

Copy link
Copy Markdown

⚠️It was bun runtime for me. Replaced bun runtime with node and it works. Could not find any settings for bun regarding this.⚠️

🔴 This was all bun runtime's fault from now on

Well, nothing works for me.
I always got 413 response.
I'm using BUN as runtime, express v4.21.2 and body-parser v1.20.3

Try 1

import express from "express";
import { json, urlencoded } from "body-parser";
// import { json, urlencoded } from "express"; // I have tried this as well

const payloadLimit = 2000 * 1024 * 1024; // 2000 MB in bytes
const appOptions = {
  extended: true,
  limit: payloadLimit,
  parameterLimit: payloadLimit,
};

app.use(express.json());
app.use(json({ ...appOptions }));
app.use(urlencoded({ ...appOptions }));

Try 2

import express from "express";
import { json, urlencoded } from "body-parser";
// import { json, urlencoded } from "express"; // I have tried this as well

const payloadLimit = 2000 * 1024 * 1024; // 2000 MB in bytes
const appOptions = {
  extended: true,
  limit: payloadLimit,
  parameterLimit: payloadLimit,
};

app.use(json({ ...appOptions }));
app.use(urlencoded({ ...appOptions }));
app.use(express.json());

Try 3

import express from "express";
import { json, urlencoded } from "body-parser";
// import { json, urlencoded } from "express"; // I have tried this as well

const payloadLimit = 2000 * 1024 * 1024; // 2000 MB in bytes
const appOptions = {
  extended: true,
  limit: payloadLimit,
  parameterLimit: payloadLimit,
};

app.use(json({ ...appOptions }));
app.use(urlencoded({ ...appOptions }));
app.use(express.json());

Try 4

import express from "express";
// import { json, urlencoded } from "body-parser";  // I have tried this as well
import { json, urlencoded } from "express";

const payloadLimit = 2000 * 1024 * 1024; // 2000 MB in bytes
const appOptions = {
  extended: true,
  limit: payloadLimit,
  parameterLimit: payloadLimit,
};

app.use(json({ ...appOptions }));
app.use(urlencoded({ ...appOptions }));
app.use(express.json());

Try 5

import express from "express";
// import { json, urlencoded } from "body-parser";  // I have tried this as well
import { json, urlencoded } from "express";

const payloadLimit = 2000 * 1024 * 1024; // 2000 MB in bytes
const appOptions = {
  extended: true,
  limit: payloadLimit,
  parameterLimit: payloadLimit,
};

app.use(express.json());
app.use(json({ ...appOptions }));
app.use(urlencoded({ ...appOptions }));

It's just don't want to work. It works on localhost but not on server.
The proxy maintainer said that it is my app's fault.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment