Search code examples
javascriptnode.jsgetchunkednpm-request

Stuck in infinite redirect loop when http get an chunked response


i'm trying to get the body of the following url https://extranet.ores.be/de/services/price-simulation using the npm module request. The thing is , for this link, the module doesn't work properly i think. Because it continuously fail with a maxRedirects reached error. I have debuged the think and yes because the first call to the url is a response with the location header to the same url, it does an infite loop. The think is , that redirection doesn't seems to be a problem for firefox or chrome, ... The browsers are resolving it correctly. Am i missing something ? or maybe is the proxy the problem ?

Here are parts of my code :

var proxiedRequest = request.defaults({proxy: "http://proxy.xxx.xxxxxxx.be:XXXX", maxRedirects : 5})
    proxiedRequest.get(that.buildRequest(url.url), (error, response, body) => {
        let html = null;
        let status = null;
        let failed = false;

        if (!error && response.statusCode === 200 && (response.headers['content-type'].includes('text/html') || response.headers['content-type'].includes('application/xhtml+xml'))){
            html = body;
        } else if(!error && response.statusCode != 200) {
            status = response.statusCode;
            failed = true;
        }else if(error) {
            failed = true;
        }

        that.emit('getFinished', { html : html, status : status, error : error, failed : failed, url : url } );
    })

The buildRequest methode :

this.buildRequest = function(url){
        return {
            url: url.href,
            headers: {
                'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) Gecko/20100101 Firefox/54.0',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                'Connection' : 'keep-alive',
                'Accept-Encoding': this.selectAcceptEncodingHeader(url.protocol)
            },
            gzip: true,
            deflate: true
        }
    }.bind(this);
 this.selectAcceptEncodingHeader = function(protocol){
        if(protocol === 'https:'){
            return 'gzip, deflate, br';

        }

        return 'gzip, deflate';
    }.bind(this);

I tried to use multipart but it's for request, not response.

Any ideas ? thanks by advance


Solution

  • The browsers handle the given url correctly because cookies are enabled by default unlike the module request from node.

    Try this for your proxied request:

    var proxiedRequest = request.defaults({
        proxy: "http://proxy.xxx.xxxxxxx.be:XXXX",
        maxRedirects : 5,
        jar: true // enable cookie
    });