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

Internal Server Error on Non-POST w/ Body #53882

Open
1 task done
HassanAbouelela opened this issue Aug 10, 2023 · 14 comments · May be fixed by #54466
Open
1 task done

Internal Server Error on Non-POST w/ Body #53882

HassanAbouelela opened this issue Aug 10, 2023 · 14 comments · May be fixed by #54466
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.

Comments

@HassanAbouelela
Copy link

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

@HassanAbouelela HassanAbouelela added the bug Issue was opened via the bug report template. label Aug 10, 2023
@github-actions github-actions bot added the Runtime Related to Node.js or Edge Runtime with Next.js. label Aug 10, 2023
@mogzol
Copy link

mogzol commented Aug 10, 2023

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

@mogzol
Copy link

mogzol commented Aug 11, 2023

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.

@davecarlson
Copy link
Contributor

I did a PR for this yesterday, it's been merged overnight. Can you try by tagging next to the canary branch ( branch, not release as it's not made one yet)

@davecarlson
Copy link
Contributor

try
v13.4.14-canary.2

@HassanAbouelela
Copy link
Author

I believe that's #53843? I linked it in the original issue but the link broke. I believe that should fix the second two curl examples in the reproduction instructions, but the first will still fail.

@HassanAbouelela
Copy link
Author

Tested with canary.2. The examples with empty bodies are fixed. The first is still broken.

@davecarlson
Copy link
Contributor

Tested with canary.2. The examples with empty bodies are fixed. The first is still broken.
Thats good - just my PR only addressed empty content lengths i'm afraid.

There's a semi-related discussion i found on undici : nodejs/undici#35

@thienguen
Copy link

Also encountered this, have this have a work around. Though its funny that I try to do a POSTbut actually DELETE-ing something instead

realzam added a commit to realzam/next.js that referenced this issue Aug 23, 2023
fix 'UND_ERR_REQ_CONTENT_LENGTH_MISMATCH' when in a GET or HEAD api method provide a body

ISSUES CLOSED: vercel#53882
@Paget96
Copy link

Paget96 commented Aug 24, 2023

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

@bkamapantula
Copy link

I encountered the same issue on the latest version.

Downgraded the version to 13.4.12 as noted here: #53882 (comment), that worked.

@meotimdihia
Copy link

meotimdihia commented Sep 10, 2023

Did we have any way to print which code lines or requests caused the problem?
Currently, it is impossible to debug.
Please share the solution on the self-hosted server if you had it.


edit: nvm, we just need to try to catch the requests.

@rahadkc
Copy link

rahadkc commented Oct 8, 2023

I also facing the same issue but It seems it's fixed in 13.5.4 version. Thanks

@pranjanai
Copy link

I am on 13.5.6 and still having the same issue, "UND_ERR_REQ_CONTENT_LENGTH_MISMATCH" causing with "server action" while "revalidatingTag or revalidatingPath".

@milindgoel15
Copy link

Any update on this issue? Any possible workarounds?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Issue was opened via the bug report template. Runtime Related to Node.js or Edge Runtime with Next.js.
Projects
None yet
10 participants