Skip to content

Is printing the raw http request from the built in http/https libraries not supported? #56820

Answered by sunnypatell
1mike12 asked this question in General
Discussion options

You must be logged in to vote

You're correct that Node.js does not provide a built-in way to retrieve the fully formatted HTTP request before it is sent. This limitation arises because requests are streamed rather than constructed as a full string in memory.

However, you can achieve similar behavior using a writable stream as an interceptor. By using http.ClientRequest's event listeners, you can capture chunks as they are written. Here's a simple approach:

const http = require('http');
const { Writable } = require('stream');

class CaptureStream extends Writable {
  constructor() {
    super();
    this.chunks = [];
  }
  _write(chunk, encoding, callback) {
    this.chunks.push(chunk);
    callback();
  }
  getRawRequest

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by 1mike12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants