Support Topics Documentation Slack YouTube Blog

hitting a 3rd party API from servercode works locally but not deployed


(Gal Cohen) #1
  • Worked before I updated to backendless 4*
  • currently on servercode 4.3.1
  • API uses HTTPS

anything else I’m missing? Any debugging suggestions?

Thanks!


(Sergey Kukurudzyak) #2

Hello,

what errors do you get?


(Gal Cohen) #3

No errors. That’s the problem, nothing in the logs, either. using https module in node to make the request. I got log statements on error, end and and data event callbacks, they never even get called.

Do I have to whitelist 3rd party domains I want to hit maybe?


(Vitaly Vengrov) #4

Are you sure, you have declared your Promise to be resolved only after you got the response ?

ServerCode works well in the DEBUG mode, but does not when deployed to the Cloud


(Gal Cohen) #5

If that were the case, the code would not work locally, and it does. It also worked unchanged back before the migration so it feels more like a permission or timeout issue than anything else.


(Gal Cohen) #6

If that were the case, the code would not work locally, and it does. It also worked unchanged back before the migration so it feels more like a permission or timeout issue than anything else.


(Vitaly Vengrov) #7

permission or timeout issue would raise an error

in case of too early promise resolving, the code will work locally but not in cloud.
Could you please show us your code ?


(Gal Cohen) #8

function request(options, callback) {
console.log(‘request log’, options);

const req =  https.request(httpOpts, function(res){
    console.log('request log', 'result', res);
    let chunks = [];

    res.on('data', function(chunk) {
        console.log('request log', 'chunk:' , chunk);
        chunks.push(chunk);
    });

    res.on('end', function() {
        const body = Buffer.concat(chunks).toString();
        console.log('request log', 'end:' , res, body);
        callback(null, res, body);
    });
});

req.on('error', function(e) {
    console.log('request log', 'error:' , e);
    callback(e, null, null);
});

if (options.body) {
    req.write(JSON.stringify(options.body));
}

req.end();

}


(Vitaly Vengrov) #9

Gal, could you please send an email to support@backendless.com with the cURL of the query triggering this servercode and that 3rd party API request, so we could trace its execution.

Btw, there is a Backendless.Request utility class available in servercode environment which can be used to send API requests.

So, you can send the request as this

Backendless.Request.post(url, body).then(response => {
console.log(response);
})