next.js: Internal Server Error on Non-POST w/ Body

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: win32
      Arch: x64
      Version: Windows 11 Home
    Binaries:
      Node: 20.5.1
      npm: N/A
      Yarn: N/A
      pnpm: N/A
    Relevant Packages:
      next: 13.4.14-canary.1
      eslint-config-next: 13.4.13
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.1.6
    Next.js Config:
      output: N/A

Which area(s) of Next.js are affected? (leave empty if unsure)

Middleware / Edge (API routes, runtime)

Link to the code that reproduces this issue or a replay of the bug

https://codesandbox.io/p/sandbox/agitated-yonath-cy9nps

To Reproduce

  1. Start the server provided in the codesandbox demo
  2. Open a new terminal and run any of the following:
  • curl localhost:3000/example -X GET -H "Content-Type: text/plain" -d "Test"
  • curl localhost:3000/example -X GET -H "Content-Type: text/plain" -d ""
  • curl localhost:3000/example -X DELETE -H "Content-Type: text/plain" -d ""
  1. Observe curl output as: Internal Server Error. Observe exception in server.

Describe the Bug

Sending a request to an API route with a body and without the POST method causes an internal server error.

Exception:

TypeError: fetch failed
    at Object.fetch (/project/sandbox/node_modules/next/dist/compiled/undici/index.js:1:26669)
    at async invokeRequest (/project/sandbox/node_modules/next/dist/server/lib/server-ipc/invoke-request.js:21:12)
    at async invokeRender (/project/sandbox/node_modules/next/dist/server/lib/router-server.js:252:29)
    at async handleRequest (/project/sandbox/node_modules/next/dist/server/lib/router-server.js:445:24)
    at async requestHandler (/project/sandbox/node_modules/next/dist/server/lib/router-server.js:462:13)
    at async Server.<anonymous> (/project/sandbox/node_modules/next/dist/server/lib/start-server.js:116:13) {
  cause: RequestContentLengthMismatchError: Request body length does not match content-length header
      at write (/project/sandbox/node_modules/next/dist/compiled/undici/index.js:1:67105)
      at _resume (/project/sandbox/node_modules/next/dist/compiled/undici/index.js:1:66726)
      at resume (/project/sandbox/node_modules/next/dist/compiled/undici/index.js:1:65413)
      at connect (/project/sandbox/node_modules/next/dist/compiled/undici/index.js:1:65301) {
    code: 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH'
  }
}

Expected Behavior

Sending a GET or similar request with a body may or may not be correct behavior, however it should not be an error for the server. The server should either ignore the body, or return a 400 (or whatever is deemed appropriate).

Probably related: #deleteerro

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

Occurs in dev and production builds

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 6
  • Comments: 17 (4 by maintainers)

Commits related to this issue

Most upvoted comments

Any update on this issue? Any possible workarounds?

I’ve had to roll back to 13.4.12 until this issue is fixed, since my app is running behind a load balancer which always adds the Content-Length header.

I am on 13.4.12 and still have the same issue

I’ve had to roll back to 13.4.12 until this issue is fixed, since my app is running behind a load balancer which always adds the Content-Length header.

Also encountering this. It also happens with GET requests without a body if there is a Content-Length: 0 header, which should be valid