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

Already on GitHub? Sign in to your account

How to abort a request? #772

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

Comments

Projects
None yet
4 participants

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!

Owner

mikeal commented Jan 15, 2014

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

We should get this in to the documentation.

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!

Owner

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 Jan 16, 2014

Thanks a lot!

Thanks a lot!

@ismriv ismriv referenced this issue in request/request-promise Jan 25, 2016

Closed

How to abort a pending request? #85

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment