request.get(options).pipe(dest);
The problem is that, if the 'get' comes back with, say, a 404, and the
origin server includes an HTML page for the 404 error, that page is
"helpfully" piped to 'dest' as if it were the content I was trying to
retrieve. That's not what I want. (In fact, I need to create my own
JSON error response.)
I've been unable to find any way of getting around this. I tried
providing an onResponse function and only piping from there, like
this:
options.onResponse = function (error, response) {
// ...
if (response.statusCode >= 200 && response.statusCode < 300) {
// ...
req.pipe(dest);
}
// ...
};
req = request.get(options);
but then 'request' complains "You cannot pipe after the response event".
How can I pipe back the response only when the request was successful,
and create my own response content on error? Is there a way to do this
and retain the magic of 'request'?
--
Martin Cooper
> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en
>
It does occur to me that the restriction you're hitting here is really in the wrong place, there is no reason, technically, that you can't pipe() after the response event so long as no data has been emitted so what I should really do is move the restriction to after a data event has been emitted so you can use onResponse like you're attempting to.
-Mikeal
That would be nice. Certainly if I comment out that particular check /
throw, I get the behaviour I'm looking for. At that point, it works
like a charm.
--
Martin Cooper
Oh, sure, I realise that. I was just confirming that removing the
check from that particular spot resolves the issue for my use case.
Thanks!
--
Martin Cooper
What's different is that I would lose the magic of 'request' - things
like automatic redirect handling, proxy handling, header transfer,
multipart, to name only a few. The 'request' library wraps all that up
very nicely into an easy to use package.
> var req = http.request(opts)
> req.end()
> req.on('response', function (res) {
> //check status & start pipe
> })
> now an onResponse: property is really ugly, it's not idiomatic node js.
> if request was to support this, that is it should do it by remitting
> 'response' like in core.
The onResponse property is part of the 'request' API today.
--
Martin Cooper
you could do this easily with core http, it's really not that different.var req = http.request(opts)
req.end()req.on('response', function (res) {//check status & start pipe})now an onResponse: property is really ugly, it's not idiomatic node js.
https://github.com/mikeal/request/commit/248e9d65e73ac868948a82d07feaf33387723a1d
Please try this out, i have a minimal test but I want some more feedback before I push it in a release.
I also added a response event so you can do.
request.post(url).on('response', function () {
})
Should make Dominic happy :)
-Mikeal
Works like a charm. Thank you!
--
Martin Cooper