Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes... this is off the top of my head. Note, that I'm proxying to port 80 since most websites serve on port 80. Change the code for your specific use case. This is in CoffeeScript.</p> <p><strong>Log Request Headers:</strong></p> <pre><code>fs = require('fs') httpProxy = require('http-proxy') fsw = fs.createWriteStream('myfile.txt', flags: 'a', mode: 0666, encoding: 'utf8') server = httpProxy.createServer (req, res, proxy) -&gt; req.connection.pipe(fsw) #runs in parallel with the proxy... don't you love Node.js? proxy.proxyRequest(req, res, { host: require('url').parse(req.url).hostname, port: 80 }) server.listen(8080) </code></pre> <p><a href="http://coffeescript.org/#try%3afs%20%3D%20require%28%27fs%27%29%0AhttpProxy%20%3D%20require%28%27http-proxy%27%29%0A%0Afsw%20%3D%20fs.createWriteStream%28%27myfile.txt%27%2C%20flags%3A%20%27a%27%2C%20mode%3A%200666%2C%20encoding%3A%20%27utf8%27%29%0Aserver%20%3D%20httpProxy.createServer%20%20%28req%2C%20res%2C%20proxy%29%20-%3E%0A%20%20%20%20%20%20%20%20%20%20%20req.connection.pipe%28fsw%29%20%23runs%20in%20parallel%20with%20the%20proxy...%20don%27t%20you%20love%20Node.js%3F%0A%20%20%20%20%20%20%20%20%20%20%20proxy.proxyRequest%28req%2C%20res%2C%20%7B%0A%20%20%20%20%20%20%20%20%20%20%20%20%20host%3A%20require%28%27url%27%29.parse%28req.url%29.hostname%2C%0A%20%20%20%20%20%20%20%20%20%20%20%20%20port%3A%2080%0A%20%20%20%20%20%20%20%20%20%20%20%7D%29%0Aserver.listen%288080%29" rel="nofollow">JS Translation</a></p> <p>Put 'localhost' and port 8080 for your browser proxy. Does this work for you?</p> <p><strong>Log Request Body:</strong></p> <pre><code>fs = require('fs') httpProxy = require('http-proxy') server = httpProxy.createServer (req, res, proxy) -&gt; body = '' req.on 'data', (chunk) -&gt; body += chunk req.on 'end', -&gt; fs.writeFile('mybody.txt', body, 'utf8') proxy.proxyRequest(req, res, { host: require('url').parse(req.url).hostname, port: 80 }) server.listen(8080) </code></pre> <p>I tested this and can confirm that it logs the body of a POST/PUT.</p> <p><strong>Log Response Body:</strong></p> <pre><code>fsw = fs.createWriteStream('myfile.txt', flags: 'a', mode: 0666, encoding: 'utf8') server = httpProxy.createServer (req, res, proxy) -&gt; oldwrite = res.write res.write = (data, encoding, fd) -&gt; fsw.write(data) res.write = oldwrite res.write(data, encoding, fd) res.write = oldwrite #&lt;--- patch again before we leave the method proxy.proxyRequest(req, res, { host: require('url').parse(req.url).hostname, port: 80 }) server.listen(8080) </code></pre> <p>Might not be the cleanest way, but I can confirm that it works.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload