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

Empty JSON object does not get fixed by fixRequestBody #639

Closed
2 tasks done
mhassan1 opened this issue Jul 14, 2021 · 0 comments · Fixed by #640
Closed
2 tasks done

Empty JSON object does not get fixed by fixRequestBody #639

mhassan1 opened this issue Jul 14, 2021 · 0 comments · Fixed by #640
Labels

Comments

@mhassan1
Copy link
Contributor

mhassan1 commented Jul 14, 2021

yarn why http-proxy-middleware OR npm ls http-proxy-middleware output (mask private folder names with *****)

├─ server@workspace:packages/server
│  └─ http-proxy-middleware@npm:2.0.1 (via npm:^2.0.1)

Describe the bug (be clear and concise)

The fixRequestBody utility contains a check for empty objects that results in early return:

if (!requestBody || !Object.keys(requestBody).length) {
return;
}

When an empty object request (curl ... -H 'content-type: application/json' -d '{}') is made to a proxy with onProxyReq: fixRequestBody, the request hangs because the target is expecting content-length: 2 but never receives any payload because of the early return. See Steps to Reproduce, below.

Am I missing a reason why empty objects get special handling?

Step-by-step reproduction instructions

1. Run the following script:

const express = require('express')
const bodyParser = require('body-parser')
const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware')

const proxyApp = express()
proxyApp.use(bodyParser.json())
proxyApp.use(createProxyMiddleware({
  target: 'http://localhost:8081',
  onProxyReq: fixRequestBody
}))
proxyApp.listen(8080)

const app = express()
app.use((req, res, next) => console.log(req.headers) || next())
app.use(bodyParser.json())
app.post('/', (req, res) => res.json(req.body))
app.listen(8081)

2. `curl localhost:8080 -XPOST -H 'content-type: application/json' -d '{"hello":"there"}'` -> `{"hello":"there"}`
3. `curl localhost:8080 -XPOST -H 'content-type: application/json' -d '{}'` -> hangs

Expected behavior (be clear and concise)

I expect an empty object JSON request to be proxied with the request body fixed, the same way as a non-empty object JSON request.

What http-proxy-middleware configuration are you using?

const { createProxyMiddleware, fixRequestBody } = require('http-proxy-middleware')

app.use(createProxyMiddleware({
  target: 'http://localhost:8081',
  onProxyReq: fixRequestBody
}))

What OS/version and node/version are you seeing the problem?

MacOS 11.4 and Node 14.15.1

Additional context (optional)

No response

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

Successfully merging a pull request may close this issue.

1 participant