chimurai / http-proxy-middleware

:zap: The one-liner node.js http-proxy middleware for connect, express, next.js and more
MIT License
10.62k stars 825 forks source link

POST request looses body #913

Closed pascalverlinden closed 1 year ago

pascalverlinden commented 1 year ago

Checks

Describe the bug (be clear and concise)

It seems this is an "old" issue, but I ran into it and am not able to fix it with the comments I found in the existing issues.

I have 2 express apps, all running in a (single) docker container. The first app serves as a gateway, which should proxy incoming requests to the target server.

GET requests are proxied correctly. POST requests seem to loose their body.

Step-by-step reproduction instructions

The gateway:

const express = require('express');
const { createProxyMiddleware } = require('http-proxy-middleware');
const app = express();
const proxy = createProxyMiddleware({
  target: 'http://localhost:3001',
  logLevel: 'debug'
});
app.use(proxy);  
app.use(express.json());
app.listen(3000, () => {
  console.log('GATEWAY listening on port 3000!');
});

The target server:

const express = require('express');
const axios = require('axios');
const app = express();
app.get('/', (req, res) => {
  res.send('Hello World!\n');
});
app.post('/', (req, res) => {
  console.log('******** service received POST %s', JSON.stringify(req.body, null, 2));
  res.send('SERVICE GOT A POST!\n');
})
app.listen(3001, () => {
  console.log('SERVICE listening on port 3001!');
}); 

Start both apps, then send a request:

// GET
curl  http://localhost:3000 
// this will print:
// [HPM] GET / -> http://localhost:3001
// Hello World!

// POST
curl -d "{'type': 'pencil'}" http://localhost:3000
// this will print:
// [HPM] POST / -> http://localhost:3001
// ******** service received POST undefined
// SERVICE GOT A POST!

Expected behavior (be clear and concise)

The target server should receive the POST request with the original body

How is http-proxy-middleware used in your project?

npm ls http-proxy-middleware
gateway@1.0.0 /home/app
`-- http-proxy-middleware@2.0.6

What http-proxy-middleware configuration are you using?

{
  target: 'http://localhost:3001',
  logLevel: 'debug'
}

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

Everything is running in a docker container with Debian GNU/Linux 10 

System:
    OS: Linux 5.10 Debian GNU/Linux 10 (buster) 10 (buster)
    CPU: (4) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
    Memory: 3.25 GB / 5.81 GB
    Container: Yes
    Shell: 5.0.3 - /bin/bash
  Binaries:
    Node: 14.21.2 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 6.14.17 - /usr/local/bin/npm
  Managers:
    Apt: 1.8.2.3 - /usr/bin/apt
  Utilities:
    Make: 4.2.1 - /usr/bin/make
    GCC: 8.3.0 - /usr/bin/gcc
    Git: 2.20.1 - /usr/bin/git
    Mercurial: 4.8.2 - /usr/bin/hg
    Subversion: 1.10.4 - /usr/bin/svn
  Languages:
    Bash: 5.0.3 - /bin/bash
    Perl: 5.28.1 - /usr/bin/perl
    Python: 2.7.16 - /usr/bin/python
    Python3: 3.7.3 - /usr/bin/python3

Additional context (optional)

No response

chimurai commented 1 year ago

Not clear which issues you already looked at and what you already tried... Would be nice if you could list them.

Guessing you already explored https://github.com/chimurai/http-proxy-middleware/issues/40#issuecomment-163398924 and changed the order of the proxy and express.json();

Alternatively you could try to configure the fixRequestBody in onProxyReq: https://github.com/chimurai/http-proxy-middleware/tree/v2.0.6#intercept-and-manipulate-requests

pascalverlinden commented 1 year ago

Thanks @chimurai I searched the issues for the same problem and tried to change the order of the middlewares. I also tried with fixRequestBody.