Replies: 1 comment 1 reply
-
@s-sathish Hey there! It looks like you're on the right track with intercepting the HTTPS requests and modifying them, but there's a small issue causing the original request to be made in addition to your modified request. Let's refine your code a bit to ensure only the modified request is executed. Here's the updated version of your Interceptor class: import { Helper } from '@internal/helper';
const https = require('https');
export class Interceptor {
private originalHttpRequest: any;
private readonly helper: Helper;
constructor() {
this.helper = new Helper();
}
startInterceptor() {
this.originalHttpRequest = https.request;
https.request = this.intercept.bind(this);
}
intercept(options: any, callback: any) {
if (options.method === 'POST') {
const originalRequestBodyChunks: Buffer[] = [];
// Return a Promise to handle async modification
return new Promise((resolve, reject) => {
const originalRequest = this.originalHttpRequest(options, (response: any) => {
response.on('data', () => {}); // Dummy listener to avoid errors
response.on('end', () => {
// Suppress the original response
});
});
originalRequest.on('error', reject);
originalRequest.write = (data: any, encoding: any) => {
let requestData = data;
if (typeof requestData === 'string') {
requestData = Buffer.from(requestData, encoding);
}
originalRequestBodyChunks.push(requestData);
};
originalRequest.end = () => {
options.body = Buffer.concat(originalRequestBodyChunks).toString();
this.executeModifiedRequest(options, callback)
.then(resolve)
.catch(reject);
};
// If the request body is sent directly
if (options.body) {
originalRequest.write(options.body);
}
// Explicitly call end to finish the request
originalRequest.end();
});
} else {
return this.executeModifiedRequest(options, callback);
}
}
private async executeModifiedRequest(options: any, callback: any) {
try {
const newOptions = this.helper.getInterceptedRequest('https', options);
const req = this.originalHttpRequest(newOptions, callback);
if (newOptions.body) {
req.write(newOptions.body);
}
req.end();
return req;
} catch (error) {
// eslint-disable-next-line no-console
console.log('Error executing the modified request: ', error);
throw error;
}
}
} Handling the original request involves creating it and collecting its body in the originalRequestBodyChunks array, adding a dummy listener to the original response to suppress it and avoid errors, and overriding the end method of the original request to trigger the creation and execution of the modified request. The executeModifiedRequest method prepares and sends the modified request using the helper's data, concatenates the original request body, and assigns it to options.body before sending the modified request, explicitly calling req.end() to finish it. The intercept function returns a Promise to handle asynchronous behavior, ensuring the original request does not send data before the modified request is prepared. By making these changes, the original request is suppressed, and only the modified request is executed. |
Beta Was this translation helpful? Give feedback.
-
My application uses NodeJS https request (https://nodejs.org/api/https.html) to make outgoing calls.
I need to intercept it and first fetch the request body. Once the request is fetched, I'm preparing a modified request and want the modified request to be executed instead of the original intercepted request.
The problem is that, it makes two calls. One with the modified request and one with the original request.
This is what I have so far.
Interceptor -
Start the interceptor in the application -
Any help on the same ?
Beta Was this translation helpful? Give feedback.
All reactions