[Issue]RequestContentLengthMismatchError: Request body length does not match content-length header
yaohuangguan opened this issue · 11 comments
Prerequisites
- I have written a descriptive issue title
- I have searched existing issues to ensure the bug has not already been reported
Fastify version
3.22
Plugin version
No response
Node.js version
14.x
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
centos 7
Description
{
"statusCode": 500,
"code": "UND_ERR_REQ_CONTENT_LENGTH_MISMATCH",
"error": "Internal Server Error",
"message": "Request body length does not match content-length header"
}
Steps to Reproduce
just register the plugin as doc shows.
Expected Behavior
should proxy correctly
i also checkout a info from fastify-reply-from, they say https://github.com/fastify/fastify-reply-from#onresponserequest-reply-res
remove content-length header, i did so in replyOptions => onResponse hook provided, but it didnt work.
The request you are proxying is advertising a different content-length than what it is there. Could you upload an example that we can use to reproduce the problem?
i can give you code block. its hard to reproduce an exact same example since we cant access the upstream here.
app.register(httpProxy, {
upstream: process.env.URl,
prefix: '/api'
})is there any possible way to try to solve this problem? also i can give you is that the proxy request is not actually hitting the upstream server, it fails before.
The request you are sending has a mismatch between the Content-Length they are advertising and the data on the wire. The easiest way for you to fix this is to fix your client so it follows HTTP/1.1.
It's also possible to extend fastify-reply-from and fastify-http-proxy to pass through the strictContentLength option of Undici. https://undici.nodejs.org/#/docs/api/Client?id=parameter-clientoptions
I haven't got time/bandwidth to look into it, so a PR would be nice in case it's needed.
Where should i modifiy strictContentLength this option?
Also you mentioned fix my client, what do you exactly suggest me doing? because my fastify client is just using that plugin. i have no idea how to fix it, i think my fastify client and upstream both follow HTTP/1.1
Who is calling the fastify-http-proxy?
fastify -> fastify-http-proxy -> upstream server
is fastify-http-proxy using the uidici for fetching data from upstream server? can fastify-http-proxy expose strictContentLength for us to set in replyOption?
if i understand correctly, the second method you mentioned is to set strictContentLength to false?
i looked into reply-from and http-proxy source code and fixed this issue. but its still messging out the error, which not a big deal i guess.
For any passerbys from NextJS receiving a similar error, try switching from fetch to axios.
For a full solution, see here: https://janac.medium.com/nextjs-fails-with-und-err-req-content-length-mismatch-after-redirect-from-server-0acdc0bfb194