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.

How can I print out request bodies

See original GitHub issue

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);

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:9 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
jrustcommented, Sep 11, 2018

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.

2reactions
danday74commented, Mar 21, 2018
const anyBody = require('body/any')
onProxyReq(proxyReq, req, res) {
    anyBody(req, res, function (err, body) {
        if (err) console.error(err)
        console.log(body)
    })
})
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to extract HTTP response body from a Python requests ...
I tested: r = requests.get("http://www.google.com") print(r.content). And it returned plenty of content. Check the url, try "http://www.google.com".
Read more >
Postman Tutorial Part 51 -Printing Request Body & Response ...
To print just put in console.log(). To extract value from JSON body, you can write json path which we have seen already. You...
Read more >
How to print the request Body in SOAP Message? - ServiceNow
You can try to print the request body once you add the line which executed the API i.e. after. var response = s.execute();....
Read more >
Python print response body | Example code
Simple use requests.get() method to get all body content and use response.json() to get JSON data. Don't forget to install and import the....
Read more >
How do i print the request body? - Help - Caddy Community
I am debugging my http2-https requests that is sending a POST request with Transfer-encoding as chunked. I want to write the whole request...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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