I am writing a small app that uses some SDKs that in turn call external APIs.
What I want to do is to see what requests are being made by these SDKs, and what responses do they get.
I was thinking that I could override http/s.request, print out the request that is going out. To get the response I would, however, have to intercept http.IncomingMessage.
So far I have:
const originalRequest = https.request.bind(https)
https.request = (...args) => {
console.log('Making https request for:')
console.log(JSON.stringify(args, null, 2))
const req = originalRequest(...args)
const originalOn = req.on.bind(req)
req.on = (name, handler) => {
if (name === 'response') {
// do something here?
}
return originalOn(name, handler)
}
return req
}
However, I have no idea how to finish this off :/
Any ideas? Thanks :)