Skip to content
This issue has been moved to a discussionGo to the discussion

How can I print out request bodies #177

Closed
@garyns

Description

@garyns

Hi, I'm trying to create a small development proxy to print out to the console headers, query params, and body.

But I can't get the body and prioxy to work together, and all my research had not given me any obvious solution.

In the code below, when I enable bodyParsers, I get the body printed to the console, but any PUT or POST request that contains a body ends up hanging. With no bodyParsers, req.body is undefined (as pre express docs) and the proxied request completes successfully.

Any advise on how I might approach this?

Thanks in advance.

var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var proxy = require('http-proxy-middleware');

var apiProxy = proxy({
    target: 'http://localhost:7040',
    changeOrigin: true,
    onError: function(err, req, res) {
        console.error(err.message);
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end(err.message);
    }
});

// When enabled I get body output, but server/proxy hangs and request never completes.
app.use(bodyParser.raw());
app.use(bodyParser.text());
app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

app.use(function(req, res, next) {

    console.log('\n\nHeaders');
    console.log(req.headers);

    console.log('\nQuery');
    console.log(req.query);

    console.log('\nBody');
    console.log(req.body);

    next();
});

app.use(apiProxy);
app.listen(8888);

Activity

chimurai

chimurai commented on Jul 5, 2017

@chimurai
Owner

mount your apiProxy before mounting the bodyParser middlewares should fix the issue.

garyns

garyns commented on Jul 12, 2017

@garyns
Author

Thanks for the suggestion. I did try this. I get a working proxy, but get 'undefined' for req.body (probably because then bodyParsers are now after my app.use() that dumps the data.)

If I put apiProxy before my app.use(), my app.use() never gets called.

garyns

garyns commented on Jul 12, 2017

@garyns
Author

Update - I had success using the npm 'body' package rather 'body-parser'.

chnfeeeeeef

chnfeeeeeef commented on Sep 28, 2017

@chnfeeeeeef

@garyns work for me

anurag87

anurag87 commented on Nov 10, 2017

@anurag87

@garyns can you tell the sample how it worked for you. When I am using npm body still my socket is hanging.

algaly

algaly commented on Dec 13, 2017

@algaly

can you please show an example of how to use body ?
thanks

irfanka

irfanka commented on Feb 8, 2018

@irfanka

Hi @chimurai

Would you mind adding the "mount your apiProxy before mounting the bodyParser middlewares" nugget to the README file?

I just spent three days trying to figure out why my proxy was hanging, until I stumbled upon this issue.
Might save someone else the trouble...

Thanks,

danday74

danday74 commented on Mar 21, 2018

@danday74
const anyBody = require('body/any')
onProxyReq(proxyReq, req, res) {
    anyBody(req, res, function (err, body) {
        if (err) console.error(err)
        console.log(body)
    })
})
jrust

jrust commented on Sep 11, 2018

@jrust

Found that it is possible to do it with the bodyParser middleware by following this example which shows the need to re-stream the body in the onProxyReq method.

Repository owner locked and limited conversation to collaborators on Apr 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @jrust@algaly@garyns@chimurai@chnfeeeeeef

        Issue actions

          How can I print out request bodies · Issue #177 · chimurai/http-proxy-middleware