Skip to content

Empty JSON object does not get fixed by fixRequestBody #639

@mhassan1

Description

@mhassan1

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions