How can I print out request bodies
See original GitHub issueHi, 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:
- Created 6 years ago
- Comments:9 (1 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.