TIL: Accessing Request Body from Response using nodejs Request Package
This is one of those things where the answer was so obvious that I spent 2 hours looking for a different answer before finally realizing it was right in front of me all along. For a while now I’ve been trying to figure out if it was possible to access the request content in the response call back when using the request
package in nodejs. I thought it would be a pretty common question, but evidently only me and 2 other people have ever asked it (or we’re both just really bad at reading the documentation).
For me, I needed to be able to correlate messages along a call flow. In order to do so, I needed to be able to log a record when my response came back. The problem was, I also needed to log off part of what was in the initial request message.
Turns out, you can access all of the request object right inside the response! Yeah, go figure. response.request
will get you access to a whole slew of things, like response.request.uri.path
, response.request.body
, response.request.headers
, you name it!
Here’s a simple little program I put together to demonstrate it:
|
|
You can run the test by sending something through curl, like curl -X POST http://localhost:5555/ -H 'Content-Type: application/json' -d '{"email":"test@test.com", "password":"password"}'
.
💚