node.jshttphttpshttp-status-code-502

bad gateway when sending GET request to google


as shown in the code posted below, i have a method named createOptionsFor and it returns options object. as shown in the constants sections below, i am trying to connect to translate.google. when i call connect,which is posted below as well, the following if-statement executed:

if (response.statusCode !== 200) {
    console.error(debugTag,"Did not get an OK from the server. Code:",response.statusCode);
    console.error(debugTag,"Did not get an OK from the server. Msg:",response.statusMessage);
    // console.error(debugTag,"Did not get an OK from the server. options:",options);
    response.resume();
    return;
}

and the error i am getting is:

RasDaRESTAPI:: Did not get an OK from the server. Code: 502
RasDaRESTAPI:: Did not get an OK from the server. Msg: Bad Gateway

i would like to know please why i can not connect successfully and how to solve that error

code:

export default class RasDaRESTAPI {
    constructor() {
        this.options = undefined
        this.agent = new HttpsProxyAgent('http://proxy.xxxx.xx:8000');
    }

    createOptionsFor(rasDaLayer, startDate, endDate, easting, northing, userName, password) {
        let auth = 'Basic ' + Buffer.from(userName + ':' + password).toString('base64');
        return this.options = {
            agent: this.agent,
            protocol: rasDaRes.PROTOCOL_HTTPS,
            host: rasDaRes.HOST,
            path: encodeURI(
                rasDaRes.PATH
            ),
            port:8000,
            method:'GET',
            headers: {
                'Content-Type': 'application/json'
              }
        };
    }
    
    connect(options) {
        const resCallback = (response)=>{
            if (response.statusCode !== 200) {
                console.error(debugTag,"Did not get an OK from the server. Code:",response.statusCode);
                console.error(debugTag,"Did not get an OK from the server. Msg:",response.statusMessage);
                // console.error(debugTag,"Did not get an OK from the server. options:",options);
                response.resume();
                return;
            }
            var body = '';
            response.on('data', (chunk)=>{
                body = body + chunk;
            });
        
            response.on('end', ()=>{
                console.info(debugTag,'response-body: ',body)
                onResult(response.statusCode, body);
            });
        }

        const req = https.request(options, resCallback);
        req.on('error', (err) => {
        console.error("problem with request.e.:", err);
        console.error("problem with request.e.stack:", err.stack);
    });
    req.end();
    }
}

constants

function define(name, value) {
Object.defineProperty(rasDaRes , name, {
    value:      value,
    enumerable: true,
    writable: false
});
}

export let rasDaRes = {};

define('PROTOCOL_HTTPS','https:')
define('HOST','translate.google.com')
define('PATH','/?hl=de')

Solution

  • I found the answer by help of one of my colleagues. The ‘port’ option/key is the options object is redundant as it is already set through the proxy/agent.

    Just removed the port key/option from the options object