Recently, I've switched to using requestjs
and so far it's been working well for me. However, I'm curious if there's a way to pipe in a similar manner to handling promises:
function foo(url) {
return request({ "url": url, "json": true })
}
let result = foo("https://jsonplaceholder.typicode.com/posts/1")
result.pipe(process.stdout)
result.on('data', console.log)
- Using
pipe
, I can't directly access the object itself, which limits my ability to only stream the body. - Additionally, the
on
method doesn't adhere to my specifications for parsing the response as JSON.
As a result, I'm unable to simply extract the JSON data I receive. While I could use JSON.parse
, I'm hoping to find a more straightforward way of handling the response (similar to promises) as I feel like there might be a greater solution that I'm overlooking.