1

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 :)

Daniel Gruszczyk
  • 5,379
  • 8
  • 47
  • 86
  • maybe this can help you https://stackoverflow.com/a/14126090/575643 – Idemax Oct 22 '19 at 13:57
  • im sure this can https://expressjs.com/en/4x/api.html#app.all – Idemax Oct 22 '19 at 13:58
  • It doesn't, I am not creating express app. I may have structured the question badly. Say in my app I use AWS SDK, when I call dynamodb.putItem(), this in turn somewhere in the inner workings of the SDK calls nodes https.request. I want to intercept this request and the subsequent response. – Daniel Gruszczyk Oct 22 '19 at 15:06
  • I see... If you are using an VM to host your app you must to watch HTTP/HTTPS as a proxy so you must to host a proxy to be able to route every call and response... – Idemax Oct 22 '19 at 16:19

0 Answers0