Getting Error:( Error in establishing connection) in server.js HTTP request

Hi Team,

I gone through with Error in establishing connection related threads here but i can't find any solution to solve this error.

my use case is to make http request to fetch tickets in my fresh desk account i use $reques API to make http request in my server.js .i run my app as a custom app.
i getting ** { status: 502, headers: {}, response: ‘Error in establishing connection’, errorSource: ‘APP’, attempts: 1 }** error in response

my fdk version is : 8.6.7
my node version is : v14.21.2

I added whitelisted domain also

This is my server.js code

var base64 = require('base-64');
 

exports = {


onTicketUpdateCallback: function(args) {
              //  console.log('Hello ' + args['data']['requester']['name']);
               
        let status=args['data']['ticket']['status'];
        let subject=args['data']['ticket']['subject'];
        let apprvalStatus=args['iparams']['approvalflow'];
        let api_key=args['iparams']['api_key'];
        let domain=args['iparams']['domain'];
        let to_mail=args['iparams']['Tomail'];
        let from_mail=args['iparams']['FromMail'];
        let email_body=args['iparams']['body'];
     console.log(api_key)
     console.log(domain)
     console.log(to_mail)
     console.log(from_mail)
     console.log(email_body)
     console.log(subject)

        if(apprvalStatus==status){
            console.log("i'm in IF condition");

          let  headers= {
              Authorization: `Basic ${base64.encode(api_key)}`
            }
         
            let options ={
                      
              headers:headers,
            }
            console.log(options)

            let url =`${domain}.freshdesk.com/api/v2/tickets `;
            console.log(url)

   $request.get(url, options)
  .then(
    function(data) {
      //handle "data"
      //"data" is a json string with status, headers, and response.
      console.log(data)
    },
    function(error) {
      //handle failure
      console.log(error)
    }
  );

}
else{
    console.log("not working")
}
             
        
    },

};

Kindly refer this logs too


Thank you

Hi @Kithiyon ,

Good day!

There are few possible reasons for the same.

  1. Due to request timeout, which is generally 5-10 seconds
  2. Dur to VPN limitation
  3. Due to provider network restrictions - if using a private network and outbound public calls are restricted

Could you please check if there are any such occurring on your end. If not kindly share the HAR logs

Additionally, kindly check this issue which is similar to what you faced.

We generally recommend to use Request Methods. Also there are some sample apps as well.

Regards,
Thakur

Thank you @Thakur_Ganeshsingh

My issue is not similar to This issue and i using $request method to make api call .
here i shared my HAR logs
vtecknologies-support.freshdesk.com_Archive [23-02-02 18-18-29].har (61.1 KB)

Hi @Thakur_Ganeshsingh ,Team

Kindly reply for my last reply

Hello @Thakur_Ganeshsingh @Saif @Santhosh @Raviraj

Kindly response my post. i posted 3 days before still i waiting for your replies. Thank you

Hi @Kithiyon ,

I have gone through the HAR logs, however, I don’t see any 502 in the logs shared. Could you please share the logs with the error specified?

Regards,
Thakur

Thank you @Thakur_Ganeshsingh

I little confused here. This event triggers when i update tickets in my freshdesk account so which browser tab i should send? ticket page in my fresh desk account or installation page .

It will be tickets page in your Freshdesk account tab

1 Like

Hi @Thakur_Ganeshsingh

Here i shared HAR file for your reference
Network_Archive [23-02-06 12-04-45].log (61.9 KB)

This is my logs in installation configuration page

This is my server.js code

Hi @Kithiyon ,

The code and logs look good, theres nothing wrong as such.
Could you please share fdk logs if possible?

NODE_DEBUG=fdk fdk run

Regards,
Thakur

Thank you @Thakur_Ganeshsingh
Where i will get fdk logs
I executed this NODE_DEBUG=fdk fdk run in command prompt i get

‘NODE_DEBUG’ is not recognized as an internal or external command,
operable program or batch file.

I have fdk.log file in my log folder are you mention this one?

Yeah, could you please share the FDK logs?

Hi @Thakur_Ganeshsingh

yeah sure,

fdk.log (1.8 MB)

The logs are also clean except the part where it says using deprecated version of FDK. Could you please try updating the fdk version?

@devrels - inputs?

Regards,
Thakur

Hi @Thakur_Ganeshsingh

I updated my fdk version. i currently using 8.6.7

node version v14.21.2

Still i facing same issue

It looks like the iparam named domain provides the fully-qualified domain name (FQDN) and not just the subdomain. (Notice the URL mentioned in the original screenshot at Feb 2, 2023, 2:10:42 PM.)

Change the url variable’s assignment to:

 let url = `${domain}/api/v2/tickets`;
2 Likes

Thanks @kaustavdm

I completely missed that. Good observation.

@Kithiyon - The URL generated is https://******.freshdesk.com.freshdesk.com/api/v2/tickets hence it is throwing 502.

Regards,
Thakur

Thank you @Thakur_Ganeshsingh @kaustavdm

Me also, I changed my code as you mentioned in last post. i getting


response instead of tickets payload

That log is truncated, @Kithiyon, so you don’t see the response. You need to call data.response to access the response. The object returned from $request.get(url, options) has these properties:

  • status: HTTP status code. number
  • headers: HTTP headers. object with each response header as key with its corresponding value.
  • response: The HTTP response body as a string.

(I assumed you had fixed this so I did not suggest this earlier)

Your code screenshot has mixed async...await with .then() Promise chaining. You should rewrite the function using one of the following:

Option 1:

exports = {

  onTicketUpdateCallback: function (args) {
    // ... rest of the code
    $request.get(url, options)
      .then(function (data) {
        // success handler
      }, function (error) {
        // error handler
    });
  }
}

Option 2:

exports = {

  onTicketUpdateCallback: async function (args) {
    // ... rest of the code
    try {
      const res = await $request.get(url, options);
      // Add success condition here
    } catch (err) {
      // Error handler
    }
  }
}
1 Like

Thank you @kaustavdm @Thakur_Ganeshsingh

I have one more doubt. I want to send outbound mails so i make api request to this api end point but email not triggered.
I getting 400 bad request error.
I’m not sure my request payload is correct or not .

This is my server.js code

var btoa = require('btoa');

exports = {
  onTicketUpdateCallback: function (args) {

    let status = args['data']['ticket']['status'];
    let subject = args['data']['ticket']['subject'];
    let priority = args['data']['ticket']['priority'];
    let apprvalStatus = args['iparams']['approvalflow'];
    let api_key = args['iparams']['api_key'];
    let domain = args['iparams']['domain'];
    let to_mail = args['iparams']['Tomail'];
    let from_mail = args['iparams']['FromMail'];
    let email_body = args['iparams']['body'];
    console.log(api_key)
    console.log(domain)
    console.log(to_mail)
    console.log(from_mail)
    console.log(email_body)
    console.log(subject)
    console.log(priority)

    if (apprvalStatus == status) {
      console.log("i'm in IF condition");

      let payload = {
        "email": to_mail,
        "email_config_id": from_mail,
        "status": status,
        "subject": subject,
        "priority": priority,
        " description": `<div><p>${email_body}<p><div>`,
      };

      let headers = {
        Authorization: `Basic ${btoa(api_key + ":X")}`,
        "Content-Type": "application/json"
      }
      let options = {
        method: "POST",
        headers: headers,
        body: JSON.stringify(payload),
      }
      console.log(options)

      let url = `${domain}/api/v2/tickets/outbound_email`;
      console.log(url)

      $request.post(url, options)
        .then(
          function (data) {

            console.log(data.response)
          },
          function (error) {
            console.log(error)
          }
        );
    }
    else {
      console.log("not working")
    }

  },

};