Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON_SIZE_EXCEEDED error on large POST request body (JSON) #75

Open
curiousercreative opened this issue Oct 21, 2024 · 3 comments
Open

Comments

@curiousercreative
Copy link

Submitting a JSON request body of 300KB results in the below server error output:

Error: {"errors":[{"name":"JSON_SIZE_EXCEEDED","message":"json object exceeds 0B"}]}
  at getProperError (home/project/node_modules/next/dist/lib/is-error.js:41:12)
  at NextNodeServer.renderToResponseImpl (home/project/node_modules/next/dist/server/base-server.js:1968:53)
  at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
  at async NextNodeServer.pipeImpl (home/project/node_modules/next/dist/server/base-server.js:921:25)
  at async NextNodeServer.handleCatchallRenderRequest (home/project/node_modules/next/dist/server/next-server.js:272:17)
  at async NextNodeServer.handleRequestImpl (home/project/node_modules/next/dist/server/base-server.js:817:17)
  at async invokeRender (home/project/node_modules/next/dist/server/lib/router-server.js:173:21)
  at async handleRequest (home/project/node_modules/next/dist/server/lib/router-server.js:350:24)
  at async requestHandlerImpl (home/project/node_modules/next/dist/server/lib/router-server.js:374:13)
  at async Server.requestListener (home/project/node_modules/next/dist/server/lib/start-server.js:141:13)

After digging into documentation, it seems this is expected behavior and the error message is just unhelpful and the documentation is lacking in what the default limit may be set to. After the error is reproduced, you can resolve it by overriding the default jsonSize limit.

import { handle } from 'next-runtime'

export const getServerSideProps = handle({
  limits: {
    jsonSize: 9999999999,
  },
  post ({ req }) {
    console.info(req.body)

    return {
      props: {
        rawBody: req.body
      }
    }
  },
})

It would be nice to see an improved error message (which includes the actual limit) and documentation updated for what the default limits are set to. It seems that the underlying body-parser has a default limit of 100KB.

Lastly, for our simple use case, I was able to remove our app dependency on next-runtime with the following prior to discovering the jsonSize limit solution above:

export default function getServerSideProps (ctx) {
  switch (ctx.req.method) {
  case 'GET':
    return get(ctx)
  case 'POST':
    return post(ctx)
  }

  return {
    notFound: true,
  }
}

async function get ({ query }) {
  return {
    props: {
      ...query,
      payload: null,
    },
  }
}

async function post ({ query , req }) {
  // read request body
  const body: string = await new Promise(res => {
    let body = ''
    req.on('data', data => body += data)
    req.on('end', () => res(body))
  })

  return {
    props: {
      ...query,
      payload: JSON.parse(body),
    },
  }
}
@smeijer
Copy link
Owner

smeijer commented Oct 23, 2024

Got it, so the error is that the message says "exceeds 0B", while in fact it exceeded the default of 100KB which is defined by body-parser. Thanks for reporting.

@curiousercreative
Copy link
Author

@smeijer yes, but just to be very clear, it'd be helpful to mention and link out to the exact documentation for the configuration value that would need to be modified. This wouldn't be so necessary except that the traceback is totally unhelpful (perhaps the fault of Next) and doesn't lead you to next-runtime or body-parser. Debugging this was a series of shots in the dark before it was isolated to use of next-runtime and I just happened to find the applicable documentation.

@smeijer
Copy link
Owner

smeijer commented Oct 25, 2024

That makes total sense. Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants