Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

POST request looses body #913

Closed
2 tasks done
pascalverlinden opened this issue May 9, 2023 · 2 comments
Closed
2 tasks done

POST request looses body #913

pascalverlinden opened this issue May 9, 2023 · 2 comments

Comments

@pascalverlinden
Copy link

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
[email protected] /home/app
`-- [email protected]

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
Copy link
Owner

chimurai commented May 9, 2023

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 #40 (comment) 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
Copy link
Author

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

Repository owner locked and limited conversation to collaborators May 10, 2023
@chimurai chimurai converted this issue into discussion #915 May 10, 2023

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants