Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to abort a request? #772

Closed
madeinjam opened this issue Jan 15, 2014 · 5 comments
Closed

How to abort a request? #772

madeinjam opened this issue Jan 15, 2014 · 5 comments

Comments

@madeinjam
Copy link

I had been trying to abort a request for hours, can anyone help me please?

This is what I have:

app.get('/theUrl', function (req, resp) {

  var parser = new Transform();
  parser._transform = function(data, encoding, done) {
    var _this = this;
    // Process data here
  }.on("error", function(err){
        console.log(err);
      });

  var theRequest = request({
    'method' : 'GET',
    'url': '/anotherUrl',
    'headers' : {
      "ACCEPT"  : "text/event-stream"
    }
  }, function (error, response, body){
    if (error) {
      //Throw error
    }
  }).pipe(parser).pipe(resp);

  req.on("close", function(){
    parser.end();
    theRequest.abort(); //Doesn't work
  });
});

As you can see its kinda a streaming proxy, so if clients cancels the request I catch it and need to close or abort the forwarding request (theRequest).

Any ideas?

Thanks!

@mikeal
Copy link
Member

mikeal commented Jan 15, 2014

var r = request(url)
r.abort()

We should get this in to the documentation.

@madeinjam
Copy link
Author

Thanks for the reply @mikeal, I tried this:

var theRequest = request({
  'method' : 'GET',
  'url': '/anotherUrl',
  'headers' : {
    "ACCEPT"  : "text/event-stream"
  }
}, function (error, response, body){
  if (error) {
    //Handle error
  }
}).pipe(parser);

theRequest.abort();

And this error raises:

/home/vagrant/gateway/app.js:876
      theRequest.abort();
                 ^
TypeError: Object #<ServerResponse> has no method 'abort'
    at IncomingMessage.<anonymous> (/home/vagrant/gateway/app.js:876:18)
    at IncomingMessage.EventEmitter.emit (events.js:92:17)
    at abortIncoming (http.js:1912:11)
    at Socket.socket.onend (http.js:2010:7)
    at Socket.g (events.js:180:16)
    at Socket.EventEmitter.emit (events.js:117:20)
    at _stream_readable.js:920:16
    at process._tickCallback (node.js:415:13)

Could you help me please?

Thank you!

@nylen
Copy link
Member

nylen commented Jan 16, 2014

From the docs:

pipe() returns the destination stream

so you are no longer working with a Request object. In this case you're calling abort() on a ServerResponse. Do this instead:

var theRequest = request({ ... });

theRequest.pipe(parser);

theRequest.abort();

@nylen nylen closed this as completed Jan 16, 2014
@madeinjam
Copy link
Author

Thanks a lot!

@herlon214
Copy link

Thanks a lot!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants