简体   繁体   中英

Node.js http request not ending

I have a problem with node.js http.request call. I want to call external api (in my case Openstack api).The request is sent successfully and i get the response, but my request does not finish after response is received. Instead it waits for about 2 minutes and then the request ends. When do the same REST call from a REST client like Postman, there is no waiting and everything works well.

Here is my code, the function is called when I do the GET request on /auth URL.

var options = {
    host: 'someIp',
    path: '/auth',
    method: 'GET',
    headers: {
        "Host":"auth.api.someIp",
        "X-Auth-User":"username",
        "X-Auth-Key":"key"
    }
};

var request = http.request(options, function(response){
    var body = ""
    response.on('data', function(data) {
        body += data;
    });
    response.on('end', function() {            
        console.log('response end')
    });
});
request.on('error', function(e) {
    console.log('Problem with request: ' + e.message);
});
request.on('end', function() {
    console.log('request end');
});

request.end();  

This is the console log:

response end GET /auth 200 120080ms

My question is, why is request not ending when i do it with node.js http.request?

Mmh try listening for the finish event.

request.on('finish', function() {
    console.log('request end');
});

request is an instance of Incoming Message .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM