question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

POST request body is not proxied to the servers

See original GitHub issue

I have in my backend defined:

var proxy = proxyMiddleware('/api', {
        target: 'http://somehost.zero',
        proxyTable: {
            '/api/one': 'http://somehost.one',
            '/api/two': 'http://somehost.two',
        }
    });
app.use(proxy);

and when I’m proxing POST request (lets say to the ‘/api/one’: ‘http://somehost.one’ ) I don’t know why but server http://somehost.one get request without body.

Did you had similar problem?

Issue Analytics

  • State:closed
  • Created 8 years ago
  • Comments:27 (4 by maintainers)

github_iconTop GitHub Comments

378reactions
chimuraicommented, Dec 9, 2015

Did a little test and I am able to receive POST data at the target server.

I noticed you are using the body-parser middleware: https://github.com/dejewi/proxy-bug/blob/master/app.js#L16

You might want to change to order of the middlwares you are using. Try moving the http-proxy-middleware above the body-parser

source: http://stackoverflow.com/a/25651651/3841188

Hope this solves the issue.

146reactions
chimuraicommented, Sep 25, 2016

If you can’t change the order of the middleware; You can restream the parsed body before proxying the request. This should fix the POST request:

// restream parsed body before proxying
var restream = function(proxyReq, req, res, options) {
    if (req.body) {
        let bodyData = JSON.stringify(req.body);
        // incase if content-type is application/x-www-form-urlencoded -> we need to change to application/json
        proxyReq.setHeader('Content-Type','application/json');
        proxyReq.setHeader('Content-Length', Buffer.byteLength(bodyData));
        // stream the content
        proxyReq.write(bodyData);
    }
}

var apiProxy = proxyMiddleware('/api', {
    target: 'https://xxx.xxx.xxx.xxx',
    onProxyReq: restream
});


var app = express();

app.use(bodyParser.json());
app.use(bodyParser.urlencoded());
app.use(apiProxy);                // with restreaming

source: node-http-proxy/examples/middleware/bodyDecoder-middleware.js. (https://github.com/nodejitsu/node-http-proxy/pull/1027)

Read more comments on GitHub >

github_iconTop Results From Across the Web

NginX fails to pass of POST request body when proxying ...
The request is routed to the live API correctly, but the data is empty! The NginX proxy does not pass along the request...
Read more >
Set up Lambda proxy integrations in API Gateway
Learn how to configure a Lambda proxy integration request and integration response in API Gateway.
Read more >
GitHub reverse proxy fails to pass POST request properly
A reverse proxy is typically used in situations where you need to send a request to another server in your control. Github probably...
Read more >
Detect HTTPS Post Request Body (and JSON-Body)
Not enough details about your client, so it is hard to answer the question is it possible to see request before encryption at...
Read more >
Configuration of limitations on POST requests - IBM
Although the request-body-max-read stanza entry limits the amount of POST content read and processed by Reverse Proxy, it does not prevent the request,...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found